2013-12-07 17:37:32 +00:00
|
|
|
var mongoose = require('mongoose');
|
|
|
|
var bcrypt = require('bcrypt');
|
2013-11-15 16:13:21 +00:00
|
|
|
|
2013-11-14 07:29:55 +00:00
|
|
|
var userSchema = new mongoose.Schema({
|
2014-01-07 21:33:48 +00:00
|
|
|
email: { type: String, unique: true },
|
2013-11-19 18:13:41 +00:00
|
|
|
password: String,
|
2013-12-06 04:46:47 +00:00
|
|
|
|
2013-12-07 07:17:46 +00:00
|
|
|
tokens: Array,
|
2013-12-06 04:46:47 +00:00
|
|
|
provider: String,
|
2014-01-07 21:33:48 +00:00
|
|
|
facebook: { type: String, unique: true, sparse: true },
|
|
|
|
twitter: { type: String, unique: true, sparse: true },
|
|
|
|
google: { type: String, unique: true, sparse: true },
|
|
|
|
github: { type: String, unique: true, sparse: true },
|
2013-12-06 04:46:47 +00:00
|
|
|
|
|
|
|
profile: {
|
2013-12-06 05:03:07 +00:00
|
|
|
name: { type: String, default: '' },
|
2013-12-06 05:18:39 +00:00
|
|
|
gender: { type: String, default: '' },
|
2013-12-06 05:03:07 +00:00
|
|
|
location: { type: String, default: '' },
|
|
|
|
website: { type: String, default: '' },
|
2013-12-12 03:16:06 +00:00
|
|
|
picture: { type: String, default: '' }
|
2013-12-07 07:17:46 +00:00
|
|
|
}
|
2013-11-14 07:29:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
userSchema.pre('save', function(next) {
|
|
|
|
var user = this;
|
2013-11-18 19:37:01 +00:00
|
|
|
var SALT_FACTOR = 5;
|
2013-11-14 07:29:55 +00:00
|
|
|
|
|
|
|
if (!user.isModified('password')) return next();
|
|
|
|
|
2013-11-18 19:37:01 +00:00
|
|
|
bcrypt.genSalt(SALT_FACTOR, function(err, salt) {
|
2013-11-14 07:29:55 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
|
|
|
|
bcrypt.hash(user.password, salt, function(err, hash) {
|
|
|
|
if (err) return next(err);
|
|
|
|
user.password = hash;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
userSchema.methods.comparePassword = function(candidatePassword, cb) {
|
|
|
|
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
|
|
|
|
if(err) return cb(err);
|
|
|
|
cb(null, isMatch);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-12-06 03:42:20 +00:00
|
|
|
module.exports = mongoose.model('User', userSchema);
|