-
Notifications
You must be signed in to change notification settings - Fork 1
/
passport.config.js
52 lines (46 loc) · 1.34 KB
/
passport.config.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
46
47
48
49
50
51
52
const passport = require("passport");
const User = require("./models/user");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
require("dotenv").config();
passport.use(User.createStrategy());
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: `${process.env.FRONTEND_URL}/auth/google/home`,
userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo",
},
function (accessToken, refreshToken, profile, cb) {
User.findOne({ username: profile.emails[0].value }, (err, found) => {
if (err) {
console.log(err);
}
if (found) {
return cb(err, found);
}
User.findOrCreate(
{
googleId: profile.id,
username: profile.emails[0].value,
name: profile.displayName,
profileUrl: profile.photos[0].value,
searchName: profile.displayName.toLowerCase(),
},
function (err, user) {
return cb(err, user);
}
);
});
}
)
);
module.exports = { passport };