preparation for code for lowercase emails and lowercase version of username to check for duplicates

This commit is contained in:
Matteo Pagliazzi
2015-10-21 11:01:06 +02:00
parent 2745a172ea
commit c7f8426bce
2 changed files with 18 additions and 8 deletions

View File

@@ -67,8 +67,12 @@ api.authWithUrl = function(req, res, next) {
} }
api.registerUser = function(req, res, next) { api.registerUser = function(req, res, next) {
var regEmail = RegexEscape(req.body.email), var regEmail = RegexEscape(req.body.email);
regUname = RegexEscape(req.body.username); var regUname = RegexEscape(req.body.username);
// Get the lowercase version of username to check that we do not have duplicates
// So we can search for it in the database and then reject the choosen username if 1 or more results are found
var lowerCaseUsername = req.body.username.toLowerCase();
async.auto({ async.auto({
validate: function(cb) { validate: function(cb) {
if (!(req.body.username && req.body.password && req.body.email)) if (!(req.body.username && req.body.password && req.body.email))
@@ -95,7 +99,8 @@ api.registerUser = function(req, res, next) {
auth: { auth: {
local: { local: {
username: req.body.username, username: req.body.username,
email: req.body.email, lowerCaseUsername: lowerCaseUsername, // Store the lowercase version of the username
email: req.body.email.toLowerCase(), // Store email as lowercase
salt: salt, salt: salt,
hashed_password: utils.encryptPassword(req.body.password, salt) hashed_password: utils.encryptPassword(req.body.password, salt)
}, },
@@ -266,15 +271,19 @@ var invalidPassword = function(user, password){
} }
api.changeUsername = function(req, res, next) { api.changeUsername = function(req, res, next) {
var user = res.locals.user;
var username = req.body.username;
async.waterfall([ async.waterfall([
function(cb){ function(cb){
User.findOne({'auth.local.username': RegexEscape(req.body.username)}, {auth:1}, cb); User.findOne({'auth.local.username': RegexEscape(username)}, {auth:1}, cb);
}, },
function(found, cb){ function(found, cb){
if (found) return cb({code:401, err: "Username already taken"}); if (found) return cb({code:401, err: "Username already taken"});
if (invalidPassword(res.locals.user, req.body.password)) return cb(invalidPassword(res.locals.user, req.body.password)); if (invalidPassword(user, req.body.password)) return cb(invalidPassword(user, req.body.password));
res.locals.user.auth.local.username = req.body.username; user.auth.local.username = username;
res.locals.user.save(cb); user.auth.local.lowerCaseUsername = username.toLowerCase();
user.save(cb);
} }
], function(err){ ], function(err){
if (err) return err.code ? res.json(err.code, err) : next(err); if (err) return err.code ? res.json(err.code, err) : next(err);

View File

@@ -66,7 +66,8 @@ var UserSchema = new Schema({
email: String, email: String,
hashed_password: String, hashed_password: String,
salt: String, salt: String,
username: String username: String,
lowerCaseUsername: String // Store a lowercase version of username to check for duplicates
}, },
timestamps: { timestamps: {
created: {type: Date,'default': Date.now}, created: {type: Date,'default': Date.now},