-
Notifications
You must be signed in to change notification settings - Fork 4
/
passport.js
46 lines (36 loc) · 1.13 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
44
45
const passport = require('passport');
const { Strategy } = require('passport-google-oauth20');
const User = require('./server/models/User');
passport.serializeUser((user, done) => {
done(null, user.googleId);
});
passport.deserializeUser(async (id, done) => {
const user = await User.findOne({ googleId: id });
done(null, user.googleId);
});
// Set up new Google Auth Passport strategy, fetch keys from .env file
passport.use(
new Strategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: process.env.AUTH_PATH,
scope: ['profile', 'email']
},
async (accessToken, refreshToken, profile, done) => {
// Verify if user exists in MongoDB
const existingUser = await User.findOne({ googleId: profile.id });
if (existingUser) {
return done(null, existingUser);
}
// If not, create new user in MongoDB
const user = await User.create({
googleId: profile.id,
username: profile.displayName,
email: profile._json.email,
photo: profile._json.picture
});
done(null, user);
}
)
);