p5.js-web-editor/server/config/passport.js

84 lines
2.9 KiB
JavaScript
Raw Normal View History

const passport = require('passport');
const GitHubStrategy = require('passport-github').Strategy;
const LocalStrategy = require('passport-local').Strategy;
2016-06-27 19:09:18 +02:00
import User from '../models/user';
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 19:09:18 +02:00
User.findOne({ email: email.toLowerCase() }, (err, 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-06-25 00:08:52 +02:00
return done(null, false, { msg: 'Invalid email or password.' });
});
2016-06-25 00:08:52 +02:00
});
}));
/**
* Sign in with GitHub.
*/
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) => {
if (req.user) {
2016-08-31 21:12:18 +02:00
// the user should actually never get here.
User.findOne({ github: profile.id }, (err, existingUser) => {
if (existingUser) {
2016-08-31 21:12:18 +02:00
return res.json({'errors', { msg: 'There is already a GitHub account that belongs to you. Sign in with that account or delete it, then link it with your current account.' }});
} else {
User.findById(req.user.id, (err, user) => {
user.email = user.email || profile._json.email;
user.github = profile.id;
user.username = user.username || profile.username;
user.tokens.push({ kind: 'github', accessToken });
user.name = user.name || profile.displayName;
user.save((err) => {
2016-08-31 21:12:18 +02:00
return res.json({'info', { msg: 'GitHub account has been linked.' }});
});
});
}
});
} else {
User.findOne({ github: profile.id }, (err, existingUser) => {
if (existingUser) {
return done(null, existingUser);
}
User.findOne({ email: profile._json.email }, (err, existingEmailUser) => {
if (existingEmailUser) {
2016-08-31 21:12:18 +02:00
return res.json('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with GitHub manually from Account Settings.' });
} else {
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) => {
2016-08-31 21:12:18 +02:00
return res.json({'info', { msg: 'Account has been created with GitHub credentials.' }});
});
}
});
});
}
}));