Guest User

Untitled

a guest
May 19th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. const mongoose = require('mongoose');
  2. const bcrypt = require('bcrypt'); //< ===========
  3.  
  4. const userSchema = new mongoose.Schema({
  5. username: {
  6. type: String,
  7. required: true,
  8. unique: true,
  9. lowercase: true, // Kyle => kyle
  10. },
  11. password: {
  12. type: String,
  13. required: true,
  14. },
  15. });
  16.  
  17. userSchema.pre('save', function(next) {
  18. bcrypt.hash(this.password, 11, (err, hash) => {
  19. if (err) {
  20. return next(err);
  21. }
  22.  
  23. this.password = hash;
  24.  
  25. return next(); // goes on to save to the db
  26. });
  27. });
  28.  
  29. userSchema.methods.isPasswordValid = function(passwordGuess) {
  30. return bcrypt.compare(passwordGuess, this.password);
  31. };
  32.  
  33. // userSchema.post('save', function(next) {
  34. // console.log('post save hook');
  35.  
  36. // next();
  37. // });
  38.  
  39. module.exports = mongoose.model('User', userSchema);
Add Comment
Please, Sign In to add comment