-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleOAuth2.0.js
32 lines (31 loc) · 929 Bytes
/
GoogleOAuth2.0.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
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const User = require("./models/User");
module.exports = (passport) => {
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/callback",
},
async function (accessToken, refreshToken, profile, cb) {
try {
let user = await User.findOne({ googleId: profile.id });
if (user) {
return cb(null, user);
} else {
const newUser = {
googleId: profile.id,
name: profile.displayName,
image: profile.photos[0].value,
};
user = await User.create(newUser);
cb(null, user);
}
} catch (err) {
console.log(err);
}
}
)
);
};