mirror of
https://github.com/mjrode/WhatToWatch.git
synced 2026-01-05 13:09:29 -06:00
37 lines
869 B
JavaScript
37 lines
869 B
JavaScript
const passport = require('passport');
|
|
const GoogleStrategy = require('passport-google-oauth20').Strategy;
|
|
const keys = require('../config/keys');
|
|
|
|
const User = mongoose.model('users');
|
|
|
|
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: keys.googleClientID,
|
|
clientSecret: keys.googleClientSecret,
|
|
callbackURL: '/auth/google/callback',
|
|
proxy: true,
|
|
},
|
|
async (accessToken, refreshToken, profile, done) => {
|
|
const existingUser = await User.findOne({googleId: profile.id});
|
|
|
|
if (existingUser) {
|
|
return done(null, existingUser);
|
|
}
|
|
|
|
const user = await new User({googleId: profile.id}).save();
|
|
done(null, user);
|
|
},
|
|
),
|
|
);
|