52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
const passport = require('passport') ;
|
|
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
|
|
|
|
|
|
const User = require('./models/User') ;
|
|
|
|
passport.serializeUser(function(user, done) {
|
|
done(null, user);
|
|
});
|
|
|
|
passport.deserializeUser(function(user, done) {
|
|
//User.findById(id, function(err, user) {
|
|
done(null, user);
|
|
//});
|
|
});
|
|
|
|
passport.use(new GoogleStrategy({
|
|
clientID: '1026548376782-5p5tjck8ffhan9l1ajhv6orr87dfkrrf.apps.googleusercontent.com',
|
|
clientSecret: 'I0L_L2XY62MjJBhLJB3eyKly',
|
|
callbackURL: "http://localhost:3000/auth/google/callback"
|
|
},
|
|
async function(accessToken, refreshToken, profile, done) {
|
|
//User.findOrCreate({ googleId: profile.id }, function (err, user) {
|
|
|
|
//return done(null, profile);
|
|
//});
|
|
try
|
|
{
|
|
let user = await User.findOne({googleId : profile.id}) ;
|
|
if(user)
|
|
{
|
|
return done(null, profile);
|
|
}
|
|
else
|
|
{
|
|
user = new User({
|
|
googleId : profile.id ,
|
|
firstName : profile.name.givenName,
|
|
lastName : profile.name.givenName ,
|
|
email : profile._json.email ,
|
|
}) ;
|
|
await user.save() ;
|
|
return done(null, profile);
|
|
}
|
|
}
|
|
catch(err)
|
|
{
|
|
return done(err, profile);
|
|
}
|
|
|
|
}
|
|
));
|