From 077154056c9a59ee2b93bbad26ea753cef29896d Mon Sep 17 00:00:00 2001 From: Robert Johnson Date: Thu, 13 Feb 2014 20:54:09 -0600 Subject: [PATCH] Check if there is already an account that isn't linked to a google account but uses the google accounts email address --- config/passport.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/config/passport.js b/config/passport.js index 8fce0eabd31..817aa2e0155 100755 --- a/config/passport.js +++ b/config/passport.js @@ -191,15 +191,22 @@ passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refre } else { User.findOne({ google: profile.id }, function(err, existingUser) { if (existingUser) return done(null, existingUser); - var user = new User(); - user.email = profile._json.email; - user.google = profile.id; - user.tokens.push({ kind: 'google', accessToken: accessToken }); - user.profile.name = profile.displayName; - user.profile.gender = profile._json.gender; - user.profile.picture = profile._json.picture; - user.save(function(err) { - done(err, user); + User.findOne({ email: profile._json.email }, function(err2, existingEmailUser) { + if(existingEmailUser) { + req.flash('errors', { msg: 'There is already an account using this google account\'s email address, login and link your account to your google account from account settings' }); + done(err2); + } else { + var user = new User(); + user.email = profile._json.email; + user.google = profile.id; + user.tokens.push({ kind: 'google', accessToken: accessToken }); + user.profile.name = profile.displayName; + user.profile.gender = profile._json.gender; + user.profile.picture = profile._json.picture; + user.save(function(err) { + done(err, user); + }); + } }); }); }