2017-02-22 20:29:35 +01:00
|
|
|
import User from '../models/user';
|
|
|
|
|
2017-04-13 18:04:10 +02:00
|
|
|
const lodash = require('lodash');
|
2016-05-17 21:50:37 +02:00
|
|
|
const passport = require('passport');
|
2016-08-31 18:28:06 +02:00
|
|
|
const GitHubStrategy = require('passport-github').Strategy;
|
2016-05-17 21:50:37 +02:00
|
|
|
const LocalStrategy = require('passport-local').Strategy;
|
|
|
|
|
|
|
|
passport.serializeUser((user, done) => {
|
|
|
|
done(null, user.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
passport.deserializeUser((id, done) => {
|
|
|
|
User.findById(id, (err, user) => {
|
|
|
|
done(err, user);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2017-01-07 00:14:42 +01:00
|
|
|
* Sign in using Email/Username and Password.
|
2016-05-17 21:50:37 +02:00
|
|
|
*/
|
|
|
|
passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
|
2017-01-07 00:14:42 +01:00
|
|
|
User.findByMailOrName(email.toLowerCase())
|
|
|
|
.then((user) => { // eslint-disable-line consistent-return
|
2016-06-25 00:08:52 +02:00
|
|
|
if (!user) {
|
|
|
|
return done(null, false, { msg: `Email ${email} not found.` });
|
|
|
|
}
|
2016-06-27 19:09:18 +02:00
|
|
|
user.comparePassword(password, (innerErr, isMatch) => {
|
2016-06-25 00:08:52 +02:00
|
|
|
if (isMatch) {
|
|
|
|
return done(null, user);
|
2016-05-17 21:50:37 +02:00
|
|
|
}
|
2016-06-25 00:08:52 +02:00
|
|
|
return done(null, false, { msg: 'Invalid email or password.' });
|
2016-05-17 21:50:37 +02:00
|
|
|
});
|
2017-01-07 00:14:42 +01:00
|
|
|
})
|
2017-02-22 20:29:35 +01:00
|
|
|
.catch(err => done(null, false, { msg: err }));
|
2016-05-17 21:50:37 +02:00
|
|
|
}));
|
|
|
|
|
2017-04-13 18:04:10 +02:00
|
|
|
/*
|
|
|
|
Input:
|
|
|
|
[
|
|
|
|
{ value: 'email@example.com', primary: false, verified: true },
|
|
|
|
{ value: 'unverified@example.com', primary: false, verified: false }
|
|
|
|
]
|
|
|
|
|
|
|
|
Output:
|
|
|
|
['email@example.com']
|
|
|
|
*/
|
|
|
|
const getVerifiedEmails = githubEmails => (
|
|
|
|
(githubEmails || [])
|
|
|
|
.filter(item => item.verified === true)
|
|
|
|
.map(item => item.value)
|
|
|
|
);
|
|
|
|
|
|
|
|
const getPrimaryEmail = githubEmails => (
|
|
|
|
(
|
|
|
|
lodash.find(githubEmails, { primary: true }) || {}
|
|
|
|
).value
|
|
|
|
);
|
|
|
|
|
2016-05-17 21:50:37 +02:00
|
|
|
/**
|
|
|
|
* Sign in with GitHub.
|
|
|
|
*/
|
2016-08-31 18:28:06 +02:00
|
|
|
passport.use(new GitHubStrategy({
|
|
|
|
clientID: process.env.GITHUB_ID,
|
|
|
|
clientSecret: process.env.GITHUB_SECRET,
|
|
|
|
callbackURL: '/auth/github/callback',
|
2017-04-13 18:04:10 +02:00
|
|
|
passReqToCallback: true,
|
|
|
|
scope: ['user:email'],
|
2016-08-31 18:28:06 +02:00
|
|
|
}, (req, accessToken, refreshToken, profile, done) => {
|
2017-02-22 20:29:35 +01:00
|
|
|
User.findOne({ github: profile.id }, (findByGithubErr, existingUser) => {
|
2016-08-31 21:30:37 +02:00
|
|
|
if (existingUser) {
|
2017-02-22 20:29:35 +01:00
|
|
|
done(null, existingUser);
|
|
|
|
return;
|
2016-08-31 21:30:37 +02:00
|
|
|
}
|
2017-04-13 18:04:10 +02:00
|
|
|
|
|
|
|
const emails = getVerifiedEmails(profile.emails);
|
|
|
|
const primaryEmail = getPrimaryEmail(profile.emails);
|
|
|
|
|
|
|
|
User.findOne({
|
|
|
|
email: { $in: emails },
|
|
|
|
}, (findByEmailErr, existingEmailUser) => {
|
2016-08-31 21:30:37 +02:00
|
|
|
if (existingEmailUser) {
|
2017-04-13 18:04:10 +02:00
|
|
|
existingEmailUser.email = existingEmailUser.email || primaryEmail;
|
2016-08-31 21:30:37 +02:00
|
|
|
existingEmailUser.github = profile.id;
|
|
|
|
existingEmailUser.username = existingEmailUser.username || profile.username;
|
|
|
|
existingEmailUser.tokens.push({ kind: 'github', accessToken });
|
|
|
|
existingEmailUser.name = existingEmailUser.name || profile.displayName;
|
2017-02-22 20:29:35 +01:00
|
|
|
existingEmailUser.save(saveErr => done(null, existingEmailUser));
|
2016-08-31 18:28:06 +02:00
|
|
|
} else {
|
2016-08-31 21:30:37 +02:00
|
|
|
const user = new User();
|
2017-04-13 18:04:10 +02:00
|
|
|
user.email = primaryEmail;
|
2016-08-31 21:30:37 +02:00
|
|
|
user.github = profile.id;
|
|
|
|
user.username = profile.username;
|
|
|
|
user.tokens.push({ kind: 'github', accessToken });
|
|
|
|
user.name = profile.displayName;
|
2017-02-22 20:29:35 +01:00
|
|
|
user.save(saveErr => done(null, user));
|
2016-08-31 18:28:06 +02:00
|
|
|
}
|
|
|
|
});
|
2016-08-31 21:30:37 +02:00
|
|
|
});
|
2016-08-31 18:28:06 +02:00
|
|
|
}));
|