From 52276b3755cf71d21ef88d6cbaf899f1bf7638c8 Mon Sep 17 00:00:00 2001 From: Sahat Yalkabov Date: Fri, 14 Feb 2014 12:23:50 -0500 Subject: [PATCH] Check if there is an account with a given email address when linking GitHub strategy. --- config/passport.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/config/passport.js b/config/passport.js index 0e9524cabe..c08609486c 100755 --- a/config/passport.js +++ b/config/passport.js @@ -108,16 +108,23 @@ passport.use(new GitHubStrategy(secrets.github, function(req, accessToken, refre } else { User.findOne({ github: profile.id }, function(err, existingUser) { if (existingUser) return done(null, existingUser); - var user = new User(); - user.email = profile._json.email; - user.github = profile.id; - user.tokens.push({ kind: 'github', accessToken: accessToken }); - user.profile.name = profile.displayName; - user.profile.picture = profile._json.avatar_url; - user.profile.location = profile._json.location; - user.profile.website = profile._json.blog; - user.save(function(err) { - done(err, user); + User.findOne({ email: profile._json.email }, function(err, existingEmailUser) { + if (existingEmailUser) { + req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with GitHub manually from Account Settings.' }); + done(err); + } else { + var user = new User(); + user.email = profile._json.email; + user.github = profile.id; + user.tokens.push({ kind: 'github', accessToken: accessToken }); + user.profile.name = profile.displayName; + user.profile.picture = profile._json.avatar_url; + user.profile.location = profile._json.location; + user.profile.website = profile._json.blog; + user.save(function(err) { + done(err, user); + }); + } }); }); }