Created
March 5, 2020 09:20
-
-
Save mphuget/9cff85bbaf6e0b09ed55e8527f2e2e53 to your computer and use it in GitHub Desktop.
pre function for UserSchema
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
// 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; | |
next(); | |
}); | |
}); | |
}) | |
// 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