Created
December 6, 2015 20:25
-
-
Save oakinogundeji/4376e2b26cceda3ddd18 to your computer and use it in GitHub Desktop.
user model module for the express 4.x local auth sample app.
This file contains 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
/** | |
*Module dependencies | |
*/ | |
var | |
mongoose = require('mongoose'), | |
bcrypt = require('bcrypt-nodejs'); | |
//============================================================================== | |
/** | |
*Module Variables | |
*/ | |
//============================================================================== | |
/** | |
*Create User Schema | |
*/ | |
var UserSchema = mongoose.Schema({ | |
username: { | |
type: String, | |
required: true | |
}, | |
email: { | |
type: String, | |
required: true, | |
unique: true | |
}, | |
password: { | |
type: String | |
}, | |
created_on: { | |
type: Date, | |
default: Date.now | |
} | |
}); | |
//============================================================================== | |
/** | |
*Schema methods | |
*/ | |
UserSchema.methods.generateHash = function(password) { | |
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); | |
}; | |
UserSchema.methods.validPassword = function(password) { | |
return bcrypt.compareSync(password, this.password); | |
}; | |
//============================================================================== | |
/** | |
*Create User Model | |
*/ | |
var UserModel = mongoose.model('User', UserSchema); | |
//============================================================================== | |
/** | |
*Export Module | |
*/ | |
module.exports = UserModel; | |
//============================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment