Guest User

Untitled

a guest
May 10th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. UserSchema.pre('save', function(next) {
  2. let user = this;
  3. if(!user.isModified('password')) return next(); //password has not modified
  4. bcrypt.genSalt(10, function (err, salt) {
  5. bcrypt.hash(user.password, salt, function (err, hash) {
  6. if(err) return next(err);
  7. user.password = hash;
  8. next();
  9. });
  10. });
  11. });
  12.  
  13.  
  14. UserSchema.methods.comparPassword = function(candidatePassword, cb) {
  15.  
  16. bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
  17. if(err) return cb(err);
  18. cb(null, isMatch);
Add Comment
Please, Sign In to add comment