2013-12-07 17:37:32 +00:00
|
|
|
var mongoose = require('mongoose');
|
2014-01-29 01:51:10 +00:00
|
|
|
var bcrypt = require('bcrypt-nodejs');
|
2014-02-03 22:50:47 +00:00
|
|
|
var crypto = require('crypto');
|
2013-11-15 16:13:21 +00:00
|
|
|
|
2013-11-14 07:29:55 +00:00
|
|
|
var userSchema = new mongoose.Schema({
|
2014-03-11 00:17:53 +00:00
|
|
|
email: { type: String, unique: true, lowercase: true },
|
2013-11-19 18:13:41 +00:00
|
|
|
password: String,
|
2013-12-06 04:46:47 +00:00
|
|
|
|
2014-02-28 00:46:56 +00:00
|
|
|
facebook: String,
|
|
|
|
twitter: String,
|
|
|
|
google: String,
|
|
|
|
github: String,
|
2014-04-22 19:00:27 +00:00
|
|
|
instagram: String,
|
2014-02-28 00:46:56 +00:00
|
|
|
linkedin: String,
|
2014-02-01 08:34:18 +00:00
|
|
|
tokens: Array,
|
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: '' }
|
2014-02-17 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
2014-02-18 07:57:57 +00:00
|
|
|
resetPasswordToken: String,
|
|
|
|
resetPasswordExpires: Date
|
2013-11-14 07:29:55 +00:00
|
|
|
});
|
|
|
|
|
2014-02-01 08:34:18 +00:00
|
|
|
/**
|
|
|
|
* Hash the password for security.
|
2014-02-18 09:05:46 +00:00
|
|
|
* "Pre" is a Mongoose middleware that executes before each user.save() call.
|
2014-02-01 08:34:18 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-14 07:29:55 +00:00
|
|
|
userSchema.pre('save', function(next) {
|
|
|
|
var user = this;
|
|
|
|
|
|
|
|
if (!user.isModified('password')) return next();
|
|
|
|
|
2014-02-18 09:05:46 +00:00
|
|
|
bcrypt.genSalt(5, function(err, salt) {
|
2013-11-14 07:29:55 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
|
2014-01-29 01:51:10 +00:00
|
|
|
bcrypt.hash(user.password, salt, null, function(err, hash) {
|
2013-11-14 07:29:55 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
user.password = hash;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-18 09:05:46 +00:00
|
|
|
/**
|
|
|
|
* Validate user's password.
|
|
|
|
* Used by Passport-Local Strategy for password validation.
|
|
|
|
*/
|
|
|
|
|
2013-11-14 07:29:55 +00:00
|
|
|
userSchema.methods.comparePassword = function(candidatePassword, cb) {
|
|
|
|
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
|
2014-02-04 00:07:17 +00:00
|
|
|
if (err) return cb(err);
|
2013-11-14 07:29:55 +00:00
|
|
|
cb(null, isMatch);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-02-03 23:03:12 +00:00
|
|
|
/**
|
2014-02-18 09:05:46 +00:00
|
|
|
* Get URL to a user's gravatar.
|
|
|
|
* Used in Navbar and Account Management page.
|
2014-02-03 23:03:12 +00:00
|
|
|
*/
|
2014-02-04 00:07:17 +00:00
|
|
|
|
2014-05-02 20:16:44 +00:00
|
|
|
userSchema.methods.gravatar = function(size) {
|
2014-02-03 23:03:12 +00:00
|
|
|
if (!size) size = 200;
|
2014-02-14 05:14:21 +00:00
|
|
|
|
2014-02-14 15:54:03 +00:00
|
|
|
if (!this.email) {
|
2014-05-02 20:16:44 +00:00
|
|
|
return 'https://gravatar.com/avatar/?s=' + size + '&d=retro';
|
2014-02-14 05:14:21 +00:00
|
|
|
}
|
|
|
|
|
2014-05-02 20:16:44 +00:00
|
|
|
var md5 = crypto.createHash('md5').update(this.email).digest('hex');
|
|
|
|
return 'https://gravatar.com/avatar/' + md5 + '?s=' + size + '&d=retro';
|
2014-02-03 22:50:47 +00:00
|
|
|
};
|
|
|
|
|
2013-12-06 03:42:20 +00:00
|
|
|
module.exports = mongoose.model('User', userSchema);
|