Check if email is already taken when creating a new local account

This commit is contained in:
Sahat Yalkabov
2014-04-25 14:29:54 -04:00
parent 18d13b67e1
commit 4cf0f654fb

View File

@ -96,18 +96,19 @@ exports.postSignup = function(req, res, next) {
password: req.body.password password: req.body.password
}); });
user.save(function(err) { User.findOne({ email: req.body.email }, function(err, existingUser) {
if (err) { if (existingUser) {
if (err.code === 11000) { req.flash('errors', { msg: 'Account with that email address already exists.' });
req.flash('errors', { msg: 'User with that email already exists.' });
}
return res.redirect('/signup'); return res.redirect('/signup');
} }
user.save(function(err) {
if (err) return next(err);
req.logIn(user, function(err) { req.logIn(user, function(err) {
if (err) return next(err); if (err) return next(err);
res.redirect('/'); res.redirect('/');
}); });
}); });
});
}; };
/** /**