passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CONSUMER_KEY,
clientSecret: process.env.GOOGLE_CONSUMER_SECRET,
callbackURL: `${process.env.GOOGLE_CALLBACK_URL}`
},
function(accessToken, refreshToken, profile, done) {
User.findOne({ email: profile.emails[0].value },
function(err, user) {
// handle immediate errors
if (err) return catchErrors(err);
// If a user with an email === google email, check for google.id
if (user) {
// if user's google schema info is undefined, set dat shit
if (user.google.id === undefined) {
user.google.id = profile.id;
user.google.token = accessToken;
user.google.email = profile.emails[0].value;
user.google.name = profile.displayName;
}
if (user.name === undefined) {
user.name = profile.givenName + profile.familyName;
}
user.save();
return done(null, user);
} else { // If no user exists with email === google email, create new User
const user = new User();
// local data
user.name = profile.displayName;
user.email = profile.emails[0].value;
user.google.id = profile.id;
user.google.token = accessToken;
user.google.name = profile.displayName;
user.google.email = profile.emails[0].value;
user.save(function(err) {
if (err) console.log(err);
return done(err, user);
});
}
}
);
}
));