Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mongoose = require('mongoose'),
- Schema = mongoose.Schema,
- bcrypt = require('bcryptjs'),
- SALT_WORK_FACTOR = 10
- var UserSchema = new Schema({
- username: { type: String, required: true, index: { unique: true } },
- password: { type: String, required: true },
- admin: {type: Boolean, default: false}
- })
- 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)
- })
- }
- module.exports = mongoose.model('User', UserSchema)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement