diff --git a/common/models/user.js b/common/models/user.js index f2e49ae252..95596ecd74 100644 --- a/common/models/user.js +++ b/common/models/user.js @@ -79,9 +79,31 @@ module.exports = function(User) { ctx.res.redirect('/email-signin'); }); - User.beforeRemote('create', function({ req }, notUsed, next) { + User.beforeRemote('create', function({ req, res }, _, next) { req.body.username = 'fcc' + uuid.v4().slice(0, 8); - next(); + if (!req.body.email) { + return next(); + } + return User.doesExist(null, req.body.email) + .then(exists => { + if (!exists) { + return next(); + } + + req.flash('error', { + msg: + `email ${req.body.email} is already in user, try signing in instead` + }); + + return res.redirect('/email-signin'); + }) + .catch(err => { + console.error(err); + req.flash('error', { + msg: 'Oops, something went wrong, please try again later' + }); + return res.redirect('/email-signup'); + }); }); User.on('resetPasswordRequest', function(info) { @@ -174,17 +196,15 @@ module.exports = function(User) { next(); }); - User.doesExist = function doesExist(username, email, cb) { + User.doesExist = function doesExist(username, email) { if (!username && !email) { - return nextTick(function() { - cb(null, false); - }); + return Promise.resolve(false); } debug('checking existence'); // check to see if username is on blacklist if (username && blacklistedUsernames.indexOf(username) !== -1) { - return cb(null, true); + return Promise.resolve(true); } var where = {}; @@ -194,19 +214,8 @@ module.exports = function(User) { where.email = email ? email.toLowerCase() : email; } debug('where', where); - User.count( - where, - function(err, count) { - if (err) { - debug('err checking existance: ', err); - return cb(err); - } - if (count > 0) { - return cb(null, true); - } - return cb(null, false); - } - ); + return User.count(where) + .then(count => count > 0); }; User.remoteMethod(