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,16 +108,23 @@ passport.use(new GitHubStrategy(secrets.github, function(req, accessToken, refre
} else { } else {
User.findOne({ github: profile.id }, function(err, existingUser) { User.findOne({ github: profile.id }, function(err, existingUser) {
if (existingUser) return done(null, existingUser); if (existingUser) return done(null, existingUser);
var user = new User(); User.findOne({ email: profile._json.email }, function(err, existingEmailUser) {
user.email = profile._json.email; if (existingEmailUser) {
user.github = profile.id; 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.' });
user.tokens.push({ kind: 'github', accessToken: accessToken }); done(err);
user.profile.name = profile.displayName; } else {
user.profile.picture = profile._json.avatar_url; var user = new User();
user.profile.location = profile._json.location; user.email = profile._json.email;
user.profile.website = profile._json.blog; user.github = profile.id;
user.save(function(err) { user.tokens.push({ kind: 'github', accessToken: accessToken });
done(err, user); 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);
});
}
}); });
}); });
} }