2016-05-17 19:50:37 +00:00
|
|
|
const passport = require('passport');
|
2016-08-31 16:28:06 +00:00
|
|
|
const GitHubStrategy = require('passport-github').Strategy;
|
2016-05-17 19:50:37 +00:00
|
|
|
const LocalStrategy = require('passport-local').Strategy;
|
|
|
|
|
2016-06-27 17:09:18 +00:00
|
|
|
import User from '../models/user';
|
2016-05-17 19:50:37 +00:00
|
|
|
|
|
|
|
passport.serializeUser((user, done) => {
|
|
|
|
done(null, user.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
passport.deserializeUser((id, done) => {
|
|
|
|
User.findById(id, (err, user) => {
|
|
|
|
done(err, user);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sign in using Email and Password.
|
|
|
|
*/
|
|
|
|
passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
|
2016-06-27 17:09:18 +00:00
|
|
|
User.findOne({ email: email.toLowerCase() }, (err, user) => { // eslint-disable-line consistent-return
|
2016-06-24 22:08:52 +00:00
|
|
|
if (!user) {
|
|
|
|
return done(null, false, { msg: `Email ${email} not found.` });
|
|
|
|
}
|
2016-06-27 17:09:18 +00:00
|
|
|
user.comparePassword(password, (innerErr, isMatch) => {
|
2016-06-24 22:08:52 +00:00
|
|
|
if (isMatch) {
|
|
|
|
return done(null, user);
|
2016-05-17 19:50:37 +00:00
|
|
|
}
|
2016-06-24 22:08:52 +00:00
|
|
|
return done(null, false, { msg: 'Invalid email or password.' });
|
2016-05-17 19:50:37 +00:00
|
|
|
});
|
2016-06-24 22:08:52 +00:00
|
|
|
});
|
2016-05-17 19:50:37 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sign in with GitHub.
|
|
|
|
*/
|
2016-08-31 16:28:06 +00:00
|
|
|
passport.use(new GitHubStrategy({
|
|
|
|
clientID: process.env.GITHUB_ID,
|
|
|
|
clientSecret: process.env.GITHUB_SECRET,
|
|
|
|
callbackURL: '/auth/github/callback',
|
|
|
|
passReqToCallback: true
|
|
|
|
}, (req, accessToken, refreshToken, profile, done) => {
|
2016-08-31 19:30:37 +00:00
|
|
|
User.findOne({ github: profile.id }, (err, existingUser) => {
|
|
|
|
if (existingUser) {
|
|
|
|
return done(null, existingUser);
|
|
|
|
}
|
|
|
|
User.findOne({ email: profile._json.email }, (err, existingEmailUser) => {
|
|
|
|
if (existingEmailUser) {
|
|
|
|
existingEmailUser.email = existingEmailUser.email || profile._json.email;
|
|
|
|
existingEmailUser.github = profile.id;
|
|
|
|
existingEmailUser.username = existingEmailUser.username || profile.username;
|
|
|
|
existingEmailUser.tokens.push({ kind: 'github', accessToken });
|
|
|
|
existingEmailUser.name = existingEmailUser.name || profile.displayName;
|
|
|
|
existingEmailUser.save((err) => {
|
|
|
|
return done(null, existingEmailUser);
|
|
|
|
});
|
2016-08-31 16:28:06 +00:00
|
|
|
} else {
|
2016-08-31 19:30:37 +00:00
|
|
|
const user = new User();
|
|
|
|
user.email = profile._json.email;
|
|
|
|
user.github = profile.id;
|
|
|
|
user.username = profile.username;
|
|
|
|
user.tokens.push({ kind: 'github', accessToken });
|
|
|
|
user.name = profile.displayName;
|
|
|
|
user.save((err) => {
|
|
|
|
return done(null, user);
|
2016-08-31 16:28:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-08-31 19:30:37 +00:00
|
|
|
});
|
2016-08-31 16:28:06 +00:00
|
|
|
}));
|