brutaljs
Version:
BrutalJS ========
31 lines (24 loc) • 590 B
text/coffeescript
mongoose = require('mongoose')
bcrypt = require('bcrypt-nodejs')
Schema = mongoose.Schema
user = new Schema({
email: {
type: String
required: true
}
password: {
type: String
required: true
}
}, {versionKey: false})
hash = (key, model) ->
if !model.isModified(key) then return
model[key] = bcrypt.hashSync(model[key])
user.pre('save', (next) ->
model = @
hash('password', model)
next(null, model)
)
user.methods.comparePassword = (candidatePassword) ->
bcrypt.compareSync(candidatePassword, @.password)
module.exports = mongoose.model('User', user)