-
Notifications
You must be signed in to change notification settings - Fork 383
/
passport-setup.js
36 lines (33 loc) · 1.21 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
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
require('dotenv').config()
passport.serializeUser(function(user, done) {
/*
From the user take just the id (to minimize the cookie size) and just pass the id of the user
to the done callback
PS: You dont have to do it like this its just usually done like this
*/
done(null, user);
});
passport.deserializeUser(function(user, done) {
/*
Instead of user this function usually recives the id
then you use the id to select the user from the db and pass the user obj to the done callback
PS: You can later access this data in any routes in: req.user
*/
done(null, user);
});
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: process.env.GOOGLE_CALLBACK
},
function(accessToken, refreshToken, profile, done) {
/*
use the profile info (mainly profile id) to check if the user is registerd in ur db
If yes select the user and pass him to the done callback
If not create the user and then select him and pass to callback
*/
return done(null, profile);
}
));