-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
43 lines (39 loc) · 1.38 KB
/
passport.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
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const User = require('./Models/userModel');
const mongoose = require('mongoose');
module.exports = function(passport){
passport.use(new GoogleStrategy({
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "/auth/google/callback"
},
async(accessToken, refreshToken, profile, cb) => {
const newUser = {
email: profile.emails[0].value,
password: profile.id,
displayName: profile.displayName,
firstName: profile.name.givenName,
lastName: profile.name.familyName
}
try {
let user = await User.findOne({ password: profile.id });
if(user) {
console.log('user found');
cb(null, user);
} else{
console.log('user created');
user = await User.create(newUser);
cb(null, user);
}
} catch(err) {
console.log(err);
}
}
));
passport.serializeUser( (user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user)=> done(err, user))
});
}