Skip to content

Commit

Permalink
perf(auth): small register perf improvement. use $or in $regex query,
Browse files Browse the repository at this point in the history
intead of two queries, so if first operand true we short-circuit second operand
  • Loading branch information
lefnire committed Feb 24, 2015
1 parent 16c4c42 commit 02901e5
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions website/src/controllers/auth.js
Expand Up @@ -65,6 +65,8 @@ api.authWithUrl = function(req, res, next) {
}

api.registerUser = function(req, res, next) {
var regEmail = RegexEscape(req.body.email),
regUname = RegexEscape(req.body.username);
async.auto({
validate: function(cb) {
if (!(req.body.username && req.body.password && req.body.email))
Expand All @@ -75,18 +77,17 @@ api.registerUser = function(req, res, next) {
return cb({code:401, err: ":email invalid"});
cb();
},
findEmail: function(cb) {
User.findOne({'auth.local.email': RegexEscape(req.body.email)}, {_id:1}, cb);
},
findUname: function(cb) {
User.findOne({'auth.local.username': RegexEscape(req.body.username)}, {_id:1}, cb);
findReg: function(cb) {
User.findOne({$or:[{'auth.local.email': regEmail}, {'auth.local.username': regUname}]}, {'auth.local':1}, cb);
},
findFacebook: function(cb){
User.findOne({_id: req.headers['x-api-user'], apiToken: req.headers['x-api-key']}, {auth:1}, cb);
},
register: ['validate', 'findEmail', 'findUname', 'findFacebook', function(cb, data) {
if (data.findEmail) return cb({code:401, err:"Email already taken"});
if (data.findUname) return cb({code:401, err:"Username already taken"});
register: ['validate', 'findReg', 'findFacebook', function(cb, data) {
if (data.findReg) {
if (regEmail.test(data.findReg.auth.local.email)) return cb({code:401, err:"Email already taken"});
if (regUname.test(data.findReg.auth.local.username)) return cb({code:401, err:"Username already taken"});
}
var salt = utils.makeSalt();
var newUser = {
auth: {
Expand Down

0 comments on commit 02901e5

Please sign in to comment.