Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // grab the things we need
- var mongoose = require("mongoose");
- var bcrypt = require("bcryptjs");
- let SALT_WORK_FACTOR = 10;
- var userSchema = new mongoose.Schema({
- username: { type: String, required: true, unique: true },
- password: { type: String, required: true }
- });
- userSchema.pre("save", function(next) {
- var user = this;
- if (!user.isModified("password")) return next();
- bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
- if (err) return next(err);
- bcrypt.hash(user.password, salt, function(err, hash) {
- if (err) return next(err);
- user.password = hash;
- next();
- });
- });
- });
- userSchema.methods.comparePassword = function(candidatePassword, cb) {
- bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
- if (err) return cb(err);
- cb(null, isMatch);
- });
- };
- var User = mongoose.model("User", userSchema);
- module.exports = User;
Add Comment
Please, Sign In to add comment