Check if there is an account with a given email address when linking GitHub strategy.

This commit is contained in:
Sahat Yalkabov
2014-02-14 12:23:50 -05:00
parent 2ba1bee9ec
commit 52276b3755

View File

@ -108,6 +108,11 @@ 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);
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;
@ -119,6 +124,8 @@ passport.use(new GitHubStrategy(secrets.github, function(req, accessToken, refre
user.save(function(err) {
done(err, user);
});
}
});
});
}
}));