Fixed password hashing in general

This commit is contained in:
Amos Haviv
2014-03-31 02:39:07 +03:00
parent c0a7c054ba
commit 47561ce3ed
2 changed files with 37 additions and 39 deletions

View File

@@ -129,56 +129,50 @@ exports.changePassword = function(req, res, next) {
var passwordDetails = req.body;
var message = null;
if (passwordDetails.currentPassword) {
if (req.user) {
User.findById(req.user.id, function(err, user) {
if (!err && user) {
if (user.authenticate(passwordDetails.currentPassword)) {
if (passwordDetails.newPassword === passwordDetails.verifyPassword) {
user.password = passwordDetails.newPassword;
if (req.user) {
User.findById(req.user.id, function(err, user) {
if (!err && user) {
if (user.authenticate(passwordDetails.currentPassword)) {
if (passwordDetails.newPassword === passwordDetails.verifyPassword) {
user.password = passwordDetails.newPassword;
user.save(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
req.login(user, function(err) {
if (err) {
res.send(400, err);
} else {
res.send({
message: 'Password changed successfully'
});
}
});
}
});
user.save(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
req.login(user, function(err) {
if (err) {
res.send(400, err);
} else {
res.send({
message: 'Password changed successfully'
});
}
});
}
});
} else {
res.send(400, {
message: 'Passwords do not match'
});
}
} else {
res.send(400, {
message: 'Current password is incorrect'
message: 'Passwords do not match'
});
}
} else {
res.send(400, {
message: 'User is not found'
message: 'Current password is incorrect'
});
}
});
} else {
res.send(400, {
message: 'User is not signed in'
});
}
} else {
res.send(400, {
message: 'User is not found'
});
}
});
} else {
res.send(400, {
message: 'Please fill current password'
message: 'User is not signed in'
});
}
};

View File

@@ -92,7 +92,11 @@ UserSchema.pre('save', function(next) {
* Create instance method for hashing a password
*/
UserSchema.methods.hashPassword = function(password) {
return crypto.pbkdf2Sync(password, this.salt, 10000, 64).toString('base64');
if (password) {
return crypto.pbkdf2Sync(password, this.salt, 10000, 64).toString('base64');
} else {
return password;
}
};
/**