Created
May 3, 2018 22:13
-
-
Save jaouadballat/8b74506cb5379bcb1cb2b04188b287c5 to your computer and use it in GitHub Desktop.
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
UserSchema.pre('save', function(next) { | |
let user = this; | |
if(!user.isModified('password')) return next(); //password has not modified | |
bcrypt.genSalt(10, function (err, salt) { | |
bcrypt.hash(user.password, salt, function (err, hash) { | |
if(err) return next(err); | |
user.password = hash; | |
next(); | |
}); | |
}); | |
}); | |
UserSchema.methods.comparPassword = function(candidatePassword, cb) { | |
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) { | |
if(err) return cb(err); | |
cb(null, isMatch); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment