Created
October 19, 2015 16:41
-
-
Save chanakaDe/315743d9c29e4ba25958 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var bcrypt = require('bcrypt-nodejs'); | |
var Schema = mongoose.Schema; | |
/** | |
* Creating user schema. | |
* @type {*|Schema} | |
*/ | |
var UserSchema = new Schema({ | |
name: String, | |
username: {type: String, required: true, index: {unique: true}}, | |
password: {type: String, required: true, select: false} | |
}); | |
/** | |
* Encrypting the user password. | |
*/ | |
UserSchema.pre('save', function (next) { | |
var user = this; | |
if (!user.isModified('password')) return next(); | |
bcrypt.hash(user.password, null, null, function (err, hash) { | |
if (err) return next(err); | |
user.password = hash; | |
next(); | |
}); | |
}); | |
UserSchema.methods.comparePassword = function (password) { | |
var user = this; | |
return bcrypt.compareSync(password, user.password); | |
}; | |
module.exports = mongoose.model('User', UserSchema); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment