freeCodeCamp/models/User.js

206 lines
4.4 KiB
JavaScript
Raw Normal View History

var bcrypt = require('bcrypt-nodejs');
2014-02-03 22:50:47 +00:00
var crypto = require('crypto');
2014-11-09 04:42:48 +00:00
var mongoose = require('mongoose');
2015-03-28 08:38:11 +00:00
require('mongoose-long')(mongoose);
2013-11-15 16:13:21 +00:00
2015-03-28 08:38:11 +00:00
var Long = mongoose.Types.Long;
var userSchema = new mongoose.Schema({
email: {
type: String,
lowercase: true,
trim: true,
sparse: true
},
password: String,
facebook: String,
twitter: String,
google: String,
github: String,
linkedin: String,
tokens: Array,
progressTimestamps: {
type: Array,
default: []
},
profile: {
username: {
type: String,
sparse: true,
lowercase: true,
trim: true
},
bio: {
type: String,
default: ''
},
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: ''
},
facebookProfile: {
type: String,
default: ''
}
},
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-09 05:42:40 +00:00
sentSlackInvite: false,
resetPasswordExpires: Date,
uncompletedBonfires: Array,
2015-03-28 08:38:11 +00:00
completedBonfires: [
{
_id: String,
name: String,
2015-03-28 08:38:11 +00:00
completedWith: String,
completedDate: Long,
solution: String
}
],
2015-02-02 07:35:27 +00:00
uncompletedCoursewares: Array,
2015-03-28 08:38:11 +00:00
completedCoursewares: [
{
completedDate: {
type: Long,
default: Date.now()
},
2015-03-28 08:38:11 +00:00
_id: String,
name: String,
completedWith: String,
solution: String,
githubLink: String,
verified: Boolean
2015-03-28 08:38:11 +00:00
}
],
completedFieldGuides: [],
uncompletedFieldGuides: [],
2015-03-28 08:38:11 +00:00
currentStreak: {
type: Number,
default: 0
},
longestStreak: {
type: Number,
default: 0
2015-04-13 05:03:26 +00:00
},
needsSomeDataModeled: { type: Boolean, default: false},
// needsMigration has been deprecated, use needsSomeDataModeled
2015-04-22 11:30:40 +00:00
needsMigration: { type: Boolean, default: true },
2015-05-01 06:41:40 +00:00
sendMonthlyEmail: { type: Boolean, default: true },
challengesHash: {},
currentChallenge: {},
completedChallenges: [
{
completedDate: Long,
_id: String,
name: String,
completedWith: String,
solution: String,
githubLink: String,
verified: Boolean,
challengeType: {
type: Number,
default: 0
}
}
],
uncompletedChallenges: Array,
});
/**
2014-11-09 04:42:48 +00:00
* Password hashing Mongoose middleware.
*/
userSchema.pre('save', function(next) {
var user = this;
if (!user.isModified('password')) { return next(); }
bcrypt.genSalt(5, function(err, salt) {
if (err) { return next(err); }
bcrypt.hash(user.password, salt, null, function(err, hash) {
if (err) { return next(err); }
user.password = hash;
next();
});
});
});
/**
2014-11-09 04:42:48 +00:00
* Helper method for validationg user's password.
*/
userSchema.methods.comparePassword = function(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) { return cb(err); }
cb(null, isMatch);
});
};
module.exports = mongoose.model('User', userSchema);