-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport-setup.js
48 lines (42 loc) · 1.35 KB
/
passport-setup.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
45
46
47
48
require('dotenv').config()
const passport = require('passport'),
GoogleStrategy = require('passport-google-oauth20'),
User = require('./models/user-model')
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id).then((user) => {
done(null, user);
});
});
passport.use(
new GoogleStrategy({
clientID: process.env.GoogleClientID,
clientSecret: process.env.GoogleClientSecret,
callbackURL: "/auth/success"
}, (accessToken, refreshToken, profile, done) => {
User.findOne({
googleID: profile.id
}).then((currentUser) => {
if (currentUser) {
done(null, currentUser)
} else {
let photos = profile.photos[0],
image = photos.value,
mail = profile.emails[0],
email = mail.value
new User({
username: profile.displayName,
googleID: profile.id,
profileImage: image.replace('sz=50','sz=200'),
basicInfo: false,
payment: false,
email: email
}).save().then((newUser) => {
done(null, newUser)
})
}
})
})
)