Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. const mongoose = require("mongoose");
  2. const Schema = mongoose.Schema;
  3. const uniqueValidator = require("mongoose-unique-validator");
  4. const bcrypt = require("bcrypt");
  5.  
  6. const UserSchema = new Schema({
  7. username: { type: String, required: true, unique: true },
  8. passwordHash: { type: String, required: true }
  9. });
  10.  
  11. UserSchema.plugin(uniqueValidator);
  12.  
  13. UserSchema.methods.validPassword = function(password) {
  14. return bcrypt.compareSync(password, this.passwordHash);
  15. };
  16.  
  17. UserSchema.virtual("password").set(function(value) {
  18. this.passwordHash = bcrypt.hashSync(value, 12);
  19. });
  20.  
  21. const User = mongoose.model("User", UserSchema);
  22. module.exports = User;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement