Skip to content

Instantly share code, notes, and snippets.

@oakinogundeji
Created December 6, 2015 20:25
Show Gist options
  • Save oakinogundeji/4376e2b26cceda3ddd18 to your computer and use it in GitHub Desktop.
Save oakinogundeji/4376e2b26cceda3ddd18 to your computer and use it in GitHub Desktop.
user model module for the express 4.x local auth sample app.
/**
*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