Last active
May 4, 2018 22:04
-
-
Save skynyrd/fc30d38b922991f2c559f86dd3759a8b to your computer and use it in GitHub Desktop.
Salting a pass using bcrypt js and mongoose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?