Skip to content

Instantly share code, notes, and snippets.

@FrancescaK
Created October 4, 2012 10:38
Show Gist options
  • Select an option

  • Save FrancescaK/3832874 to your computer and use it in GitHub Desktop.

Select an option

Save FrancescaK/3832874 to your computer and use it in GitHub Desktop.
hash the password
UserSchema.pre(‘save’, { var user = this;
// only hash the password if it has been modified (or is new)
if (!user.isModified('password')) return next();
// generate a salt
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) return next(err);
// hash the password along with our new salt
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) return next(err);
// override the cleartext password with the hashed one
user.password = hash;
next();
});
@ix-xerri
Copy link
Copy Markdown

I think there is a typo where you forgot 'function(next)'

The code is corrected in https://gist.github.com/FrancescaK/3832833

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment