From cb5d81709bfd9e69703e7c3e6f11a26997811c8e Mon Sep 17 00:00:00 2001 From: Patrick Insinger Date: Fri, 22 Dec 2017 13:41:34 -0500 Subject: [PATCH 1/2] Force emails to be unique with mongo This change forces user email addresses to be unique by using mongoose's unique constraint. --- app/server/controllers/UserController.js | 61 ++++++++++-------------- app/server/models/User.js | 1 + 2 files changed, 25 insertions(+), 37 deletions(-) diff --git a/app/server/controllers/UserController.js b/app/server/controllers/UserController.js index 4dac18817..c928edd26 100644 --- a/app/server/controllers/UserController.js +++ b/app/server/controllers/UserController.js @@ -150,48 +150,35 @@ UserController.createUser = function(email, password, callback) { return callback(err); } - User - .findOneByEmail(email) - .exec(function(err, user){ - - if (err) { - return callback(err); - } - - if (user) { + var u = new User(); + u.email = email; + u.password = User.generateHash(password); + u.save(function(err){ + if (err){ + if (err.name === 'MongoError' && (err.code === 11000 || err.code === 11001)) { return callback({ message: 'An account for this email already exists.' }); - } else { - - // Make a new user - var u = new User(); - u.email = email; - u.password = User.generateHash(password); - u.save(function(err){ - if (err){ - return callback(err); - } else { - // yay! success. - var token = u.generateAuthToken(); - - // Send over a verification email - var verificationToken = u.generateEmailVerificationToken(); - Mailer.sendVerificationEmail(email, verificationToken); - - return callback( - null, - { - token: token, - user: u - } - ); - } - - }); - } + return callback(err); + } else { + // yay! success. + var token = u.generateAuthToken(); + + // Send over a verification email + var verificationToken = u.generateEmailVerificationToken(); + Mailer.sendVerificationEmail(email, verificationToken); + + return callback( + null, + { + token: token, + user: u + } + ); + } + }); }); }; diff --git a/app/server/models/User.js b/app/server/models/User.js index bc8d4eb94..11b629399 100644 --- a/app/server/models/User.js +++ b/app/server/models/User.js @@ -155,6 +155,7 @@ var schema = new mongoose.Schema({ email: { type: String, required: true, + unique: true, validate: [ validator.isEmail, 'Invalid Email', From 94832036655dc2bc3047f8be07703c508989a297 Mon Sep 17 00:00:00 2001 From: Patrick Insinger Date: Mon, 22 Jan 2018 11:45:36 -0500 Subject: [PATCH 2/2] Add comment explaining error codes --- app/server/controllers/UserController.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/server/controllers/UserController.js b/app/server/controllers/UserController.js index c928edd26..455202e55 100644 --- a/app/server/controllers/UserController.js +++ b/app/server/controllers/UserController.js @@ -155,6 +155,7 @@ UserController.createUser = function(email, password, callback) { u.password = User.generateHash(password); u.save(function(err){ if (err){ + // Duplicate key error codes if (err.name === 'MongoError' && (err.code === 11000 || err.code === 11001)) { return callback({ message: 'An account for this email already exists.' @@ -702,4 +703,4 @@ UserController.getStats = function(callback){ return callback(null, Stats.getUserStats()); }; -module.exports = UserController; \ No newline at end of file +module.exports = UserController;