Check if there is an account with provided email when linking with Facebook strategy

This commit is contained in:
Sahat Yalkabov
2014-02-14 12:26:47 -05:00
parent 3016af41e5
commit 6b49d10fc3

View File

@ -63,8 +63,12 @@ passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, r
});
} else {
User.findOne({ facebook: profile.id }, function(err, existingUser) {
console.log(profile)
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 Facebook manually from Account Settings.' });
done(err);
} else {
var user = new User();
user.email = profile._json.email;
user.facebook = profile.id;
@ -76,6 +80,8 @@ passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, r
user.save(function(err) {
done(err, user);
});
}
});
});
}
}));
@ -204,7 +210,7 @@ passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refre
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.' });
req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with Google manually from Account Settings.' });
done(err);
} else {
var user = new User();