From 4cf0f654fb3eb6e525749a79063327b43ce5f2af Mon Sep 17 00:00:00 2001 From: Sahat Yalkabov Date: Fri, 25 Apr 2014 14:29:54 -0400 Subject: [PATCH] Check if email is already taken when creating a new local account --- controllers/user.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/controllers/user.js b/controllers/user.js index b69361ea1f..b347774b6d 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -96,16 +96,17 @@ exports.postSignup = function(req, res, next) { password: req.body.password }); - user.save(function(err) { - if (err) { - if (err.code === 11000) { - req.flash('errors', { msg: 'User with that email already exists.' }); - } + User.findOne({ email: req.body.email }, function(err, existingUser) { + if (existingUser) { + req.flash('errors', { msg: 'Account with that email address already exists.' }); return res.redirect('/signup'); } - req.logIn(user, function(err) { + user.save(function(err) { if (err) return next(err); - res.redirect('/'); + req.logIn(user, function(err) { + if (err) return next(err); + res.redirect('/'); + }); }); }); };