Created
September 12, 2016 16:04
-
-
Save unicodeveloper/d102e2e14041099251305c7b9df653bd to your computer and use it in GitHub Desktop.
Cloudinary Blog Post - Part 1
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
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