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

Commit

Permalink
Merge pull request #642 from lirantal/bugfix/password-changing-on-use…
Browse files Browse the repository at this point in the history
…r-save

Fixing user's passwords keep changing when saving the user model
  • Loading branch information
lirantal committed Jul 12, 2015
2 parents ab68fd2 + bf79c17 commit d9c15e2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/users/server/models/user.server.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var UserSchema = new Schema({
* Hook a pre save method to hash the password
*/
UserSchema.pre('save', function(next) {
if (this.password && this.password.length > 6) {
if (this.password && this.isModified('password') && this.password.length > 6) {
this.salt = crypto.randomBytes(16).toString('base64');
this.password = this.hashPassword(this.password);
}
Expand Down
10 changes: 10 additions & 0 deletions modules/users/tests/server/user.server.model.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ describe('User Model Unit Tests:', function() {
done();
});
});

it('should confirm that saving user model doesnt change the password', function(done) {
user.firstName = 'test';
var passwordBefore = user.password;
return user.save(function(err) {
var passwordAfter = user.password
passwordBefore.should.equal(passwordAfter);
done();
});
});
});

after(function(done) {
Expand Down

0 comments on commit d9c15e2

Please sign in to comment.