-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport-setup.js
35 lines (32 loc) · 949 Bytes
/
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
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const User = require('../models/user');
require('dotenv').config();
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: process.env.GOOGLE_CALLBACK_URL
},
(accessToken, refreshToken, profile, done) => {
// find or create a user with the given Google profile ID
User.findOrCreate({
where: {
googleId: profile.id
},
defaults: {
email: profile.emails[0].value,
username: profile.displayName
}
})
.then(user => done(null, user[0]))
.catch(error => done(error));
}
));
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findByPk(id)
.then(user => done(null, user))
.catch(error => done(error));
});