Skip to content

Instantly share code, notes, and snippets.

@tarusharora
Last active August 11, 2019 19:15
Show Gist options
  • Save tarusharora/ac8d3d0d140a528b174a8036d525cb53 to your computer and use it in GitHub Desktop.
Save tarusharora/ac8d3d0d140a528b174a8036d525cb53 to your computer and use it in GitHub Desktop.
User model and mongoose
const bcrypt = require('bcrypt-nodejs');
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true,
},
password: String,
});
/**
* Password hash middleware.
*/
userSchema.pre('save', function save(next) {
const user = this;
if (!user.isModified('password')) { return next(); }
bcrypt.genSalt(10, (err, salt) => {
if (err) { return next(err); }
bcrypt.hash(user.password, salt, null, (err, hash) => {
if (err) { return next(err); }
user.password = hash;
next();
});
});
});
/**
* Helper method for validating user's password.
*/
userSchema.methods.comparePassword = function comparePassword(candidatePassword) {
return new Promise((resolve, reject) => {
bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
if (err) { reject(err); }
resolve(isMatch);
});
});
};
const User = mongoose.model('User', userSchema);
module.exports = User;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment