Advertisement
Guest User

user i models liten u

a guest
Mar 2nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1.  
  2.  
  3. let mongoose = require("mongoose"),
  4. bcrypt = require('bcrypt-nodejs');
  5. // SALT_WORK_FACTOR = 10;
  6.  
  7.  
  8. /* Creating Schema for the user */
  9. let userSchema = new mongoose.Schema({
  10. username: { type: String, required: true, unique: true},
  11. password: { type: String, required: true, unique: true },
  12. createdAt: {type: Date, required: true, default: Date.now}
  13. });
  14.  
  15. /* Checks and validates the password againts the requirements */
  16. userSchema.path("password").validate(function(password) {
  17. return password.length >= 8;
  18. }, "The password must be of minimum length 8 characters.");
  19.  
  20.  
  21. userSchema.pre('save', function(next) {
  22. let user = this;
  23. bcrypt.genSalt(10, function(err, salt) {
  24. if(err) { return next(err); }
  25.  
  26. // Using https://www.npmjs.com/package/bcrypt-nodejs
  27. bcrypt.hash(user.password, salt, null, function(err, hash) {
  28. if(err) { return next(err); }
  29.  
  30. /* Makes the password to hash */
  31. user.password = hash;
  32. next();
  33. });
  34. });
  35. });
  36.  
  37.  
  38. let user = mongoose.model('user', userSchema);
  39. module.exports = user;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement