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');
|
2015-03-28 17:38:11 +09:00
|
|
|
require('mongoose-long')(mongoose);
|
2013-11-15 11:13:21 -05:00
|
|
|
|
2015-03-28 17:38:11 +09:00
|
|
|
var Long = mongoose.Types.Long;
|
2013-11-14 02:29:55 -05:00
|
|
|
var userSchema = new mongoose.Schema({
|
2015-01-21 21:32:13 -05:00
|
|
|
email: {
|
|
|
|
type: String,
|
|
|
|
lowercase: true,
|
|
|
|
trim: true,
|
|
|
|
sparse: true
|
|
|
|
},
|
|
|
|
password: String,
|
|
|
|
facebook: String,
|
|
|
|
twitter: String,
|
|
|
|
google: String,
|
|
|
|
github: String,
|
|
|
|
linkedin: String,
|
|
|
|
tokens: Array,
|
2015-04-16 23:54:26 -07:00
|
|
|
progressTimestamps: {
|
|
|
|
type: Array,
|
|
|
|
default: []
|
|
|
|
},
|
2015-01-21 21:32:13 -05:00
|
|
|
profile: {
|
|
|
|
username: {
|
|
|
|
type: String,
|
|
|
|
sparse: true,
|
|
|
|
lowercase: true,
|
|
|
|
trim: true
|
|
|
|
},
|
|
|
|
bio: {
|
|
|
|
type: String,
|
2015-04-16 23:54:26 -07:00
|
|
|
default: ''
|
2015-01-21 21:32:13 -05:00
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
gender: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
location: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
picture: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
linkedinProfile: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
githubProfile: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
codepenProfile: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
twitterHandle: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
2015-05-09 16:23:45 -07:00
|
|
|
},
|
|
|
|
facebookProfile: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
2015-01-21 21:32:13 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
portfolio: {
|
|
|
|
website1Link: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
website1Title: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
website1Image: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
website2Link: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
website2Title: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
website2Image: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
website3Link: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
website3Title: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
website3Image: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resetPasswordToken: String,
|
2015-04-08 22:42:40 -07:00
|
|
|
sentSlackInvite: false,
|
2015-01-21 23:54:59 -05:00
|
|
|
resetPasswordExpires: Date,
|
2015-01-27 01:22:02 -05:00
|
|
|
uncompletedBonfires: Array,
|
2015-03-28 17:38:11 +09:00
|
|
|
completedBonfires: [
|
|
|
|
{
|
|
|
|
_id: String,
|
2015-04-05 00:01:51 -07:00
|
|
|
name: String,
|
2015-03-28 17:38:11 +09:00
|
|
|
completedWith: String,
|
|
|
|
completedDate: Long,
|
|
|
|
solution: String
|
|
|
|
}
|
|
|
|
],
|
2015-02-01 23:35:27 -08:00
|
|
|
uncompletedCoursewares: Array,
|
2015-03-28 17:38:11 +09:00
|
|
|
completedCoursewares: [
|
|
|
|
{
|
2015-05-20 21:50:31 -04:00
|
|
|
completedDate: {
|
|
|
|
type: Long,
|
|
|
|
default: Date.now()
|
|
|
|
},
|
2015-03-28 17:38:11 +09:00
|
|
|
_id: String,
|
2015-03-29 20:39:41 +09:00
|
|
|
name: String,
|
|
|
|
completedWith: String,
|
|
|
|
solution: String,
|
2015-04-02 13:42:15 -07:00
|
|
|
githubLink: String,
|
|
|
|
verified: Boolean
|
2015-03-28 17:38:11 +09:00
|
|
|
}
|
|
|
|
],
|
2015-04-08 17:18:51 -07:00
|
|
|
completedFieldGuides: [],
|
|
|
|
uncompletedFieldGuides: [],
|
2015-03-28 17:38:11 +09:00
|
|
|
currentStreak: {
|
|
|
|
type: Number,
|
|
|
|
default: 0
|
|
|
|
},
|
|
|
|
longestStreak: {
|
|
|
|
type: Number,
|
|
|
|
default: 0
|
2015-04-13 01:03:26 -04:00
|
|
|
},
|
2015-05-05 16:12:53 -04:00
|
|
|
needsSomeDataModeled: { type: Boolean, default: false},
|
|
|
|
|
|
|
|
// needsMigration has been deprecated, use needsSomeDataModeled
|
2015-04-22 07:30:40 -04:00
|
|
|
needsMigration: { type: Boolean, default: true },
|
2015-04-24 03:04:53 -07:00
|
|
|
finishedWaypoints: { type: Boolean, default: false },
|
2015-04-30 23:41:40 -07:00
|
|
|
sendMonthlyEmail: { type: Boolean, default: true },
|
2015-05-16 00:39:43 -04:00
|
|
|
challengesHash: {},
|
2015-05-19 22:31:01 -04:00
|
|
|
currentChallenge: {},
|
|
|
|
completedChallenges: [
|
|
|
|
{
|
|
|
|
completedDate: Long,
|
|
|
|
_id: String,
|
|
|
|
name: String,
|
|
|
|
completedWith: String,
|
|
|
|
solution: String,
|
|
|
|
githubLink: String,
|
2015-05-20 21:50:31 -04:00
|
|
|
verified: Boolean,
|
|
|
|
challengeType: {
|
|
|
|
type: Number,
|
|
|
|
default: 0
|
|
|
|
}
|
2015-05-19 22:31:01 -04:00
|
|
|
}
|
|
|
|
],
|
|
|
|
uncompletedChallenges: Array
|
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) {
|
2015-01-21 21:32:13 -05:00
|
|
|
var user = this;
|
2013-11-14 02:29:55 -05:00
|
|
|
|
2015-01-21 21:32:13 -05:00
|
|
|
if (!user.isModified('password')) { return next(); }
|
2013-11-14 02:29:55 -05:00
|
|
|
|
2015-01-21 21:32:13 -05:00
|
|
|
bcrypt.genSalt(5, function(err, salt) {
|
|
|
|
if (err) { return next(err); }
|
2013-11-14 02:29:55 -05:00
|
|
|
|
2015-01-21 21:32:13 -05:00
|
|
|
bcrypt.hash(user.password, salt, null, function(err, hash) {
|
|
|
|
if (err) { return next(err); }
|
|
|
|
user.password = hash;
|
|
|
|
next();
|
|
|
|
});
|
2013-11-14 02:29:55 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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) {
|
2015-01-21 21:32:13 -05:00
|
|
|
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
|
|
|
|
if (err) { return cb(err); }
|
|
|
|
cb(null, isMatch);
|
|
|
|
});
|
2013-11-14 02:29:55 -05:00
|
|
|
};
|
|
|
|
|
2013-12-05 22:42:20 -05:00
|
|
|
module.exports = mongoose.model('User', userSchema);
|