2014-01-28 17:51:10 -08:00
|
|
|
var bcrypt = require('bcrypt-nodejs');
|
2014-02-03 17:50:47 -05:00
|
|
|
var crypto = require('crypto');
|
2014-11-08 20:42:48 -08:00
|
|
|
var mongoose = require('mongoose');
|
2013-11-15 11:13:21 -05:00
|
|
|
|
2013-11-14 02:29:55 -05:00
|
|
|
var userSchema = new mongoose.Schema({
|
2014-11-30 15:25:00 -08:00
|
|
|
email: { type: String, unique: true, lowercase: true, match: /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/ },
|
2014-11-29 22:22:27 -08:00
|
|
|
password: String,
|
2013-12-05 23:46:47 -05:00
|
|
|
|
2014-10-15 13:18:25 -07:00
|
|
|
facebook: String,
|
2014-02-27 19:46:56 -05:00
|
|
|
twitter: String,
|
2014-10-15 13:18:25 -07:00
|
|
|
google: String,
|
2014-11-29 22:22:27 -08:00
|
|
|
github: String,
|
2014-04-22 15:00:27 -04:00
|
|
|
instagram: String,
|
2014-11-29 22:22:27 -08:00
|
|
|
linkedin: String,
|
2014-02-01 03:34:18 -05:00
|
|
|
tokens: Array,
|
2014-11-06 23:01:01 -08:00
|
|
|
challengesCompleted: { type: Array, default: [] },
|
2013-12-05 23:46:47 -05:00
|
|
|
|
|
|
|
profile: {
|
2013-12-06 00:03:07 -05:00
|
|
|
name: { type: String, default: '' },
|
2013-12-06 00:18:39 -05:00
|
|
|
gender: { type: String, default: '' },
|
2013-12-06 00:03:07 -05:00
|
|
|
location: { type: String, default: '' },
|
|
|
|
website: { type: String, default: '' },
|
2014-10-13 18:00:37 -07:00
|
|
|
picture: { type: String, default: '' },
|
2014-11-30 15:25:00 -08:00
|
|
|
username: { type: String, default: '', unique: true, match: /^[a-zA-Z0-9_]+$/ }
|
2014-02-17 10:00:43 -08:00
|
|
|
},
|
|
|
|
|
2014-02-18 02:57:57 -05:00
|
|
|
resetPasswordToken: String,
|
|
|
|
resetPasswordExpires: Date
|
2013-11-14 02:29:55 -05:00
|
|
|
});
|
|
|
|
|
2014-02-01 03:34:18 -05:00
|
|
|
/**
|
2014-11-08 20:42:48 -08:00
|
|
|
* Password hashing Mongoose middleware.
|
2014-02-01 03:34:18 -05:00
|
|
|
*/
|
|
|
|
|
2013-11-14 02:29:55 -05:00
|
|
|
userSchema.pre('save', function(next) {
|
|
|
|
var user = this;
|
|
|
|
|
2014-11-08 20:42:48 -08:00
|
|
|
if (!user.isModified('password')) { return next(); }
|
2013-11-14 02:29:55 -05:00
|
|
|
|
2014-02-18 04:05:46 -05:00
|
|
|
bcrypt.genSalt(5, function(err, salt) {
|
2014-11-08 20:42:48 -08:00
|
|
|
if (err) { return next(err); }
|
2013-11-14 02:29:55 -05:00
|
|
|
|
2014-01-28 17:51:10 -08:00
|
|
|
bcrypt.hash(user.password, salt, null, function(err, hash) {
|
2014-11-08 20:42:48 -08:00
|
|
|
if (err) { return next(err); }
|
2013-11-14 02:29:55 -05:00
|
|
|
user.password = hash;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-18 04:05:46 -05:00
|
|
|
/**
|
2014-11-08 20:42:48 -08:00
|
|
|
* Helper method for validationg user's password.
|
2014-02-18 04:05:46 -05:00
|
|
|
*/
|
|
|
|
|
2013-11-14 02:29:55 -05:00
|
|
|
userSchema.methods.comparePassword = function(candidatePassword, cb) {
|
|
|
|
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
|
2014-11-08 20:42:48 -08:00
|
|
|
if (err) { return cb(err); }
|
2013-11-14 02:29:55 -05:00
|
|
|
cb(null, isMatch);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-02-03 18:03:12 -05:00
|
|
|
/**
|
2014-11-08 20:42:48 -08:00
|
|
|
* Helper method for getting user's gravatar.
|
2014-02-03 18:03:12 -05:00
|
|
|
*/
|
2014-02-03 19:07:17 -05:00
|
|
|
|
2014-05-02 16:16:44 -04:00
|
|
|
userSchema.methods.gravatar = function(size) {
|
2014-11-08 20:42:48 -08:00
|
|
|
if (!size) { size = 200; }
|
2014-02-14 00:14:21 -05:00
|
|
|
|
2014-02-14 10:54:03 -05:00
|
|
|
if (!this.email) {
|
2014-05-02 16:16:44 -04:00
|
|
|
return 'https://gravatar.com/avatar/?s=' + size + '&d=retro';
|
2014-02-14 00:14:21 -05:00
|
|
|
}
|
|
|
|
|
2014-05-02 16:16:44 -04:00
|
|
|
var md5 = crypto.createHash('md5').update(this.email).digest('hex');
|
|
|
|
return 'https://gravatar.com/avatar/' + md5 + '?s=' + size + '&d=retro';
|
2014-02-03 17:50:47 -05:00
|
|
|
};
|
|
|
|
|
2013-12-05 22:42:20 -05:00
|
|
|
module.exports = mongoose.model('User', userSchema);
|