mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-05 12:02:13 +00:00
perf(auth): small register perf improvement. use $or in $regex query,
intead of two queries, so if first operand true we short-circuit second operand
This commit is contained in:
parent
16c4c420bb
commit
02901e52fd
1 changed files with 9 additions and 8 deletions
|
|
@ -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))
|
||||
|
|
@ -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: {
|
||||
|
|
|
|||
Loading…
Reference in a new issue