Skip to content

Instantly share code, notes, and snippets.

@unicodeveloper
Created September 12, 2016 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unicodeveloper/d102e2e14041099251305c7b9df653bd to your computer and use it in GitHub Desktop.
Save unicodeveloper/d102e2e14041099251305c7b9df653bd to your computer and use it in GitHub Desktop.
Cloudinary Blog Post - Part 1
var mongoose = require('mongoose'),
bcrypt = require('bcrypt'),
userSchema = mongoose.Schema({
fullName: { type: String },
email: { type: String, required: true, unique: true, lowercase: true },
password: { type: String, required: true },
user_avatar: { type: String, default: 'http://s3.amazonaws.com/37assets/svn/765-default-avatar.png' },
registered_on: { type: Date, default: Date.now }
});
userSchema.pre('save', function(next) {
var user = this;
if (!user.isModified('password')) {
return next();
}
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(user.password, salt, function(err, hash) {
user.password = hash;
next();
});
});
});
userSchema.methods.comparePassword = function(password, done) {
bcrypt.compare(password, this.password, function(err, isMatch) {
done(err, isMatch);
});
};
module.exports = mongoose.model('User', userSchema, 'users');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment