Advertisement
Guest User

Untitled

a guest
Jul 9th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. userSchema.pre('save', function (next) {
  2. const user = this;
  3. if (this.isModified('password') || this.isNew) {
  4. bcrypt.genSalt(10, (err, salt) => {
  5. if (err) {
  6. return next(err);
  7. }
  8. bcrypt.hash(user.password, salt, (err, hash) => {
  9. if (err) {
  10. return next(err);
  11. }
  12. user.password = hash;
  13. next();
  14. });
  15. });
  16. } else {
  17. return next();
  18. }
  19. });
  20.  
  21. // Compare password input to password saved in database
  22. userSchema.methods.comparePassword = function (pw, cb) {
  23. bcrypt.compare(pw, this.password, (err, isMatch) => {
  24. if (err) {
  25. return cb(err);
  26. }
  27. cb(null, isMatch);
  28. });
  29. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement