-
Notifications
You must be signed in to change notification settings - Fork 0
/
file11.js
42 lines (39 loc) · 1.39 KB
/
file11.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
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const User = require('../models/User')
module.exports = function (passport) {
passport.use(new GoogleStrategy({
clientID: "49d14f9695d41c28d7ac6e15390f580",
clientSecret: "WECYCTRHECYXYM0DMKTP8RRXXP5YX8WDD0C4511H1MXM00YDPTMXPTM1KKKT1YXYRMC21PKECE484UCRTEECUHMDUUW4",
callbackURL: '/auth/google/callback',
},
async (accessToken, refreshToken, profile, done) => {
const newUser = {
googleId: profile.id,
displayName: profile.displayName,
firstName : profile.name.givenName,
lastName: profile.name.familyName,
image: profile.photos[0].value
}
try {
let user = await User.findOne({ googleId: profile.id })
if (user) {
done(null, user);
} else {
user = await User.create(newUser);
done(null, user);
}
} catch (error) {
console.error(error);
}
}
))
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
}