Guest User

Untitled

a guest
Jun 17th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. const mongoose = require('mongoose');
  2. const bcrypt = require('bcryptjs');
  3.  
  4. const userSchema = new mongoose.Schema({
  5. username: {type: String, required: true},
  6. password: {type: String, required: true}
  7. });
  8.  
  9. userSchema.pre('save', function(next){
  10. if (!this.isModified('password')) { return next() };
  11.  
  12. bcrypt.hash(this.password, 10)
  13. .then(digest => {
  14. this.password = digest;
  15. next();
  16. });
  17. })
  18.  
  19. const User = mongoose.model('User', userSchema);
  20.  
  21. module.exports = User;
Add Comment
Please, Sign In to add comment