Skip to content

Instantly share code, notes, and snippets.

@jaouadballat
Created May 3, 2018 22:13
Show Gist options
  • Save jaouadballat/8b74506cb5379bcb1cb2b04188b287c5 to your computer and use it in GitHub Desktop.
Save jaouadballat/8b74506cb5379bcb1cb2b04188b287c5 to your computer and use it in GitHub Desktop.
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