Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mongoose = require("mongoose");
- const Schema = mongoose.Schema;
- const uniqueValidator = require("mongoose-unique-validator");
- const bcrypt = require("bcrypt");
- const UserSchema = new Schema({
- username: { type: String, required: true, unique: true },
- passwordHash: { type: String, required: true }
- });
- UserSchema.plugin(uniqueValidator);
- UserSchema.methods.validPassword = function(password) {
- return bcrypt.compareSync(password, this.passwordHash);
- };
- UserSchema.virtual("password").set(function(value) {
- this.passwordHash = bcrypt.hashSync(value, 12);
- });
- const User = mongoose.model("User", UserSchema);
- module.exports = User;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement