-
Notifications
You must be signed in to change notification settings - Fork 4
/
passport.js
34 lines (31 loc) · 911 Bytes
/
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
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const passport = require("passport");
const User = require("./model/user.model");
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "/api/auth/google/callback",
scope: ["email", "profile"],
},
async function (accessToken, refreshToken, profile, done) {
let user = await User.findOne({ email: profile.emails[0].value });
if (!user) {
user = User.create({
username: profile.displayName,
email: profile.emails[0].value,
image: profile.photos[0].value,
});
await user.save();
}
done(null, user);
}
)
);
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});