-
Notifications
You must be signed in to change notification settings - Fork 33
/
passport.js
40 lines (37 loc) · 1.08 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
const mongoose = require("mongoose");
const User = mongoose.model("users");
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
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.GOOGLE_CLIENT_ID || "clientID",
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "clientSecret",
callbackURL: "/auth/google/callback"
},
(accessToken, refreshToken, profile, done) => {
User.findOne({ email: profile.emails[0].value }).then(existingUser => {
if (existingUser) {
done(null, existingUser);
} else {
new User({
googleId: profile.id,
name: profile.displayName,
email: profile.emails[0].value,
photo: profile.photos[0].value.split("?")[0]
})
.save()
.then(user => done(null, user));
}
});
}
)
);