Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
pre function for UserSchema
// Hash the password before we even save it to the database
UserSchema.pre('save', function(next) {
let user = this;
if (!user.isModified('local.password'))
return next();
bcrypt.genSalt(10, function(err, salt) {
if (err)
return next(err);
bcrypt.hash(user.local.password, salt, null, function(err, hash) {
if (err)
return next(err);
user.local.password = hash;
// compare password in the database and the one that the user type in
UserSchema.methods.comparePassword = function(password) {
let user = this;
return bcrypt.compareSync(password, user.local.password);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.