Guest User

Untitled

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