forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component-passport.js
74 lines (61 loc) · 1.83 KB
/
component-passport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import passport from 'passport';
import { PassportConfigurator } from 'loopback-component-passport';
import passportProviders from './passport-providers';
const passportOptions = {
emailOptional: true,
profileToUser: null
};
const fields = {
progressTimestamps: false,
completedChallenges: false,
challengeMap: false
};
PassportConfigurator.prototype.init = function passportInit(noSession) {
this.app.middleware('session:after', passport.initialize());
if (noSession) {
return;
}
this.app.middleware('session:after', passport.session());
// Serialization and deserialization is only required if passport session is
// enabled
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
this.userModel.findById(id, { fields }, (err, user) => {
if (err || !user) {
return done(err, user);
}
return this.app.dataSources.db.connector
.collection('user')
.aggregate([
{ $match: { _id: user.id } },
{ $project: { points: { $size: '$progressTimestamps' } } }
], function(err, [{ points = 1 } = {}]) {
if (err) { return done(err); }
user.points = points;
return done(null, user);
});
});
});
};
export default function setupPassport(app) {
const configurator = new PassportConfigurator(app);
configurator.setupModels({
userModel: app.models.user,
userIdentityModel: app.models.userIdentity,
userCredentialModel: app.models.userCredential
});
configurator.init();
Object.keys(passportProviders).map(function(strategy) {
var config = passportProviders[strategy];
config.session = config.session !== false;
configurator.configureProvider(
strategy,
{
...config,
...passportOptions
}
);
});
}