Skip to content

Instantly share code, notes, and snippets.

@obrientimothya
Created January 25, 2016 05:48
Show Gist options
  • Select an option

  • Save obrientimothya/46484557bbebe3b02f8e to your computer and use it in GitHub Desktop.

Select an option

Save obrientimothya/46484557bbebe3b02f8e to your computer and use it in GitHub Desktop.
jwt-api /models/person.js
// 1. Include required modules
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
mongoosePaginate = require('mongoose-paginate'),
bcrypt = require('bcryptjs');
// 2. Define the MongoDB schema for the people collection
var personSchema = new Schema({
first : {type: String, required: 'FirstNameInvalid'},
last : String,
email : {type: String, unique: true, lowercase: true, required: 'EmailInvalid'},
password : {type: String, select: false, required: 'PasswordInvalid'}
});
// 3. Paginate the results
personSchema.plugin(mongoosePaginate);
// 4. Encypt and store the person's password
personSchema.pre('save', function(next) {
var person = this;
if (!person.isModified('password')) {
return next();
}
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(person.password, salt, function(err, hash) {
person.password = hash;
next();
});
});
});
// 5. Confirm a person's password against the stored password
personSchema.methods.comparePassword = function(password, done) {
bcrypt.compare(password, this.password, function(err, isMatch) {
done(err, isMatch);
});
};
// 6. Export the Person model
module.exports = mongoose.model('Person', personSchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment