Skip to content

Instantly share code, notes, and snippets.



Last active May 4, 2018
What would you like to do?
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;

This comment has been minimized.

Copy link

@rajsaha 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