refactor(errors): remove more res.json(500) in favor of next(err)

This commit is contained in:
Tyler Renelle 2014-02-07 09:20:28 -07:00
parent 106a7fa475
commit e24d7476e8

View file

@ -39,7 +39,7 @@ api.authWithSession = function(req, res, next) { //[todo] there is probably a mo
if (!(req.session && req.session.userId))
return res.json(401, NO_SESSION_FOUND);
User.findOne({_id: uid}, function(err, user) {
if (err) return res.json(500, {err: err});
if (err) return next(err);
if (_.isEmpty(user)) return res.json(401, NO_USER_FOUND);
res.locals.user = user;
next();
@ -107,14 +107,14 @@ api.loginLocal = function(req, res, next) {
var password = req.body.password;
if (!(username && password)) return res.json(401, {err:'Missing :username or :password in request body, please provide both'});
User.findOne({'auth.local.username': username}, function(err, user){
if (err) return res.json(500,{err:err});
if (err) return next(err);
if (!user) return res.json(401, {err:"Username '" + username + "' not found. Usernames are case-sensitive, click 'Forgot Password' if you can't remember the capitalization."});
// We needed the whole user object first so we can get his salt to encrypt password comparison
User.findOne({
'auth.local.username': username,
'auth.local.hashed_password': utils.encryptPassword(password, user.auth.local.salt)
}, function(err, user){
if (err) return res.json(500,{err:err});
if (err) return next(err);
if (!user) return res.json(401,{err:'Incorrect password'});
res.json({id: user._id,token: user.apiToken});
});
@ -164,7 +164,7 @@ api.resetPassword = function(req, res, next){
hashed_password = utils.encryptPassword(newPassword, salt);
User.findOne({'auth.local.email':email}, function(err, user){
if (err) return res.json(500,{err:err});
if (err) return next(err);
if (!user) return res.send(500, {err:"Couldn't find a user registered for email " + email});
user.auth.local.salt = salt;
user.auth.local.hashed_password = hashed_password;
@ -187,18 +187,18 @@ api.changePassword = function(req, res, next) {
confirmNewPassword = req.body.confirmNewPassword;
if (newPassword != confirmNewPassword)
return res.json(500, {err: "Password & Confirm don't match"});
return res.json(401, {err: "Password & Confirm don't match"});
var salt = user.auth.local.salt,
hashed_old_password = utils.encryptPassword(oldPassword, salt),
hashed_new_password = utils.encryptPassword(newPassword, salt);
if (hashed_old_password !== user.auth.local.hashed_password)
return res.json(500, {err:"Old password doesn't match"});
return res.json(401, {err:"Old password doesn't match"});
user.auth.local.hashed_password = hashed_new_password;
user.save(function(err, saved){
if (err) res.json(500,{err:err});
if (err) next(err);
res.send(200);
})
}