Skip to content

Instantly share code, notes, and snippets.

@skynyrd
Last active May 4, 2018 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skynyrd/fc30d38b922991f2c559f86dd3759a8b to your computer and use it in GitHub Desktop.
Save skynyrd/fc30d38b922991f2c559f86dd3759a8b to your computer and use it in GitHub Desktop.
Salting a pass using bcrypt js and mongoose
// JS, Mongoose, Mongo, ExpressJS, Authentication, Security. Bcrypt
// Before saving user object
userSchema.pre('save', function(next) {
const user = this;
//Generate a salt then run callback 10: number of rounds
bcrypt.genSalt(10, function(err, salt){
if (err) {return next(err)}
//Hash the pass using salt
bcrypt.hash(user.password, salt, null, function(err, hash){
if (err) {return next(err)}
user.password = hash;
next();
});
});
});
@rajsaha
Copy link

rajsaha commented May 4, 2018

The 'null' parameter in bcrypt.hash causes the value of user.password to be undefined. Was that third parameter removed in a later version of bcryptjs?

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