-
Notifications
You must be signed in to change notification settings - Fork 0
/
passportHelper.js
44 lines (40 loc) · 1.67 KB
/
passportHelper.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
const GOOGLE_CLIENT_ID = '537458633734-qtnbigt576nqvcejt57tn8i50ahkq93n.apps.googleusercontent.com';
const GOOGLE_CLIENT_SECRET = 'Hmm2X40fvIoF9omy1I3cQrDi';
const GOOGLE_CALLBACK_URL = 'http://localhost:3000/login/google/callback';
const Strategy = require('passport-local').Strategy
const usersModel = require('./models/users');
const googleUser = require('./googleUserHelper');
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
const passport = require('passport');
module.exports = {
initPassport() {
passport.use(new Strategy({ usernameField: 'email' },
function (username, password, cb) {
usersModel.findOne({ email: username }).exec(function (err, result) {
if (err) { return cb(err); }
if (!result) { return cb(null, false); }
if (!result.validatePassword(password)) { return cb(null, false); }
return cb(null, result);
});
}));
passport.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: GOOGLE_CALLBACK_URL
},
function (accessToken, refreshToken, profile, done) {
googleUser.findOrCreateUser(profile, accessToken, done);
}
));
passport.serializeUser(function (user, cb) {
cb(null, user.id);
});
passport.deserializeUser(function (id, cb) {
usersModel.findById(id, function (err, user) {
if (err) { return cb(err); }
cb(null, user);
});
});
return passport;
}
}