2016-02-11 22:33:54 -08:00
|
|
|
import passport from 'passport';
|
|
|
|
import { PassportConfigurator } from 'loopback-component-passport';
|
|
|
|
import passportProviders from './passport-providers';
|
|
|
|
|
|
|
|
const passportOptions = {
|
|
|
|
emailOptional: true,
|
2017-07-13 11:39:07 -07:00
|
|
|
profileToUser: null
|
2016-02-11 22:33:54 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const fields = {
|
|
|
|
progressTimestamps: false,
|
2018-05-15 06:12:05 +01:00
|
|
|
completedChallenges: false
|
2016-02-11 22:33:54 -08:00
|
|
|
};
|
|
|
|
|
2018-05-15 06:12:05 +01:00
|
|
|
function getCompletedCertCount(user) {
|
|
|
|
return [
|
|
|
|
'isApisMicroservicesCert',
|
|
|
|
'is2018DataVisCert',
|
|
|
|
'isFrontEndLibsCert',
|
|
|
|
'isInfosecQaCert',
|
|
|
|
'isJsAlgoDataStructCert',
|
|
|
|
'isRespWebDesignCert'
|
|
|
|
].reduce((sum, key) => user[key] ? sum + 1 : sum, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-11 22:33:54 -08:00
|
|
|
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) => {
|
2016-04-14 17:07:40 -07:00
|
|
|
|
|
|
|
this.userModel.findById(id, { fields }, (err, user) => {
|
|
|
|
if (err || !user) {
|
2018-05-15 06:12:05 +01:00
|
|
|
user.challengeMap = {};
|
2016-04-14 17:07:40 -07:00
|
|
|
return done(err, user);
|
2016-02-11 22:33:54 -08:00
|
|
|
}
|
2018-05-15 06:12:05 +01:00
|
|
|
|
2016-04-14 17:07:40 -07:00
|
|
|
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;
|
2018-05-15 06:12:05 +01:00
|
|
|
let completedChallengeCount = 0;
|
|
|
|
let completedProjectCount = 0;
|
|
|
|
if ('challengeMap' in user) {
|
|
|
|
completedChallengeCount = Object.keys(user.challengeMap).length;
|
|
|
|
Object.keys(user.challengeMap)
|
|
|
|
.map(key => user.challengeMap[key])
|
|
|
|
.forEach(item => {
|
|
|
|
if (
|
|
|
|
'challengeType' in item &&
|
|
|
|
(item.challengeType === 3 || item.challengeType === 4)
|
|
|
|
) {
|
|
|
|
completedProjectCount++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
user.completedChallengeCount = completedChallengeCount;
|
|
|
|
user.completedProjectCount = completedProjectCount;
|
|
|
|
user.completedCertCount = getCompletedCertCount(user);
|
|
|
|
user.challengeMap = {};
|
2016-04-14 17:07:40 -07:00
|
|
|
return done(null, user);
|
|
|
|
});
|
|
|
|
});
|
2016-02-11 22:33:54 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|