Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Fixed password hashing in general
Browse files Browse the repository at this point in the history
  • Loading branch information
amoshaviv committed Mar 30, 2014
1 parent c0a7c05 commit 47561ce
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
72 changes: 33 additions & 39 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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'
});
}
});
}
});
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'
});
}
});
}
});

} 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'
});
}
};
Expand Down
6 changes: 5 additions & 1 deletion app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

/**
Expand Down

0 comments on commit 47561ce

Please sign in to comment.