-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgoogle-setup.js
More file actions
33 lines (32 loc) · 955 Bytes
/
google-setup.js
File metadata and controls
33 lines (32 loc) · 955 Bytes
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
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
require('dotenv').config({ silent: process.env.NODE_ENV === 'production' });
const User = require('./models/user');
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT,
clientSecret: process.env.GOOGLE_SECRET,
callbackURL: process.env.BASE_URL + '/auth/google/redirect',
userProfileURL: 'https://www.googleapis.com/oauth2/v3/userinfo',
},
async (token, tokenSecret, profile, done) => {
// find current user in UserModel
const currentUser = await User.findOne({
_id: profile.id,
});
// create new user if the database doesn't have this user
if (!currentUser) {
const newUser = await new User({
_id: profile.id,
displayName: profile.displayName,
role: 'Customer',
}).save();
if (newUser) {
done(null, newUser);
}
}
done(null, currentUser);
}
)
);