2016-05-13 20:04:16 +00:00
|
|
|
import mongoose from 'mongoose';
|
2017-02-22 19:29:35 +00:00
|
|
|
|
2016-06-09 20:28:21 +00:00
|
|
|
const bcrypt = require('bcrypt-nodejs');
|
2016-05-13 20:04:16 +00:00
|
|
|
|
2017-06-26 16:48:28 +00:00
|
|
|
const EmailConfirmationStates = {
|
|
|
|
Verified: 'verified',
|
|
|
|
Sent: 'sent',
|
|
|
|
Resent: 'resent',
|
|
|
|
};
|
|
|
|
|
2018-05-05 00:59:43 +00:00
|
|
|
const { Schema } = mongoose;
|
2017-02-22 19:29:35 +00:00
|
|
|
|
2016-05-13 20:04:16 +00:00
|
|
|
const userSchema = new Schema({
|
2016-06-23 22:29:55 +00:00
|
|
|
name: { type: String, default: '' },
|
2016-06-27 17:09:18 +00:00
|
|
|
username: { type: String, required: true, unique: true },
|
2016-06-23 22:29:55 +00:00
|
|
|
password: { type: String },
|
2016-10-12 16:02:46 +00:00
|
|
|
resetPasswordToken: String,
|
|
|
|
resetPasswordExpires: Date,
|
2017-06-26 16:48:28 +00:00
|
|
|
verified: { type: String },
|
|
|
|
verifiedToken: String,
|
|
|
|
verifiedTokenExpires: Date,
|
2016-06-23 22:29:55 +00:00
|
|
|
github: { type: String },
|
|
|
|
email: { type: String, unique: true },
|
|
|
|
tokens: Array,
|
2016-08-04 03:45:49 +00:00
|
|
|
preferences: {
|
2016-08-09 22:45:59 +00:00
|
|
|
fontSize: { type: Number, default: 18 },
|
2016-08-04 03:45:49 +00:00
|
|
|
indentationAmount: { type: Number, default: 2 },
|
|
|
|
isTabIndent: { type: Boolean, default: false },
|
2016-08-12 19:50:33 +00:00
|
|
|
autosave: { type: Boolean, default: true },
|
2019-03-26 19:37:44 +00:00
|
|
|
linewrap: { type: Boolean, default: true },
|
2016-08-12 19:50:33 +00:00
|
|
|
lintWarning: { type: Boolean, default: false },
|
2017-05-31 19:23:30 +00:00
|
|
|
textOutput: { type: Boolean, default: false },
|
|
|
|
gridOutput: { type: Boolean, default: false },
|
|
|
|
soundOutput: { type: Boolean, default: false },
|
2016-09-28 18:12:01 +00:00
|
|
|
theme: { type: String, default: 'light' },
|
2016-10-04 19:35:23 +00:00
|
|
|
autorefresh: { type: Boolean, default: false }
|
2016-08-04 03:45:49 +00:00
|
|
|
}
|
2019-05-02 23:10:14 +00:00
|
|
|
}, { timestamps: true, usePushEach: true });
|
2016-05-17 19:50:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Password hash middleware.
|
|
|
|
*/
|
2016-06-27 17:09:18 +00:00
|
|
|
userSchema.pre('save', function checkPassword(next) { // eslint-disable-line consistent-return
|
2016-05-17 19:50:37 +00:00
|
|
|
const user = this;
|
|
|
|
if (!user.isModified('password')) { return next(); }
|
2016-06-27 17:09:18 +00:00
|
|
|
bcrypt.genSalt(10, (err, salt) => { // eslint-disable-line consistent-return
|
2016-05-17 19:50:37 +00:00
|
|
|
if (err) { return next(err); }
|
2016-06-27 17:09:18 +00:00
|
|
|
bcrypt.hash(user.password, salt, null, (innerErr, hash) => {
|
|
|
|
if (innerErr) { return next(innerErr); }
|
2016-05-17 19:50:37 +00:00
|
|
|
user.password = hash;
|
2016-06-27 17:09:18 +00:00
|
|
|
return next();
|
2016-05-17 19:50:37 +00:00
|
|
|
});
|
|
|
|
});
|
2016-05-13 20:04:16 +00:00
|
|
|
});
|
|
|
|
|
2017-02-22 19:29:35 +00:00
|
|
|
userSchema.virtual('id').get(function idToString() {
|
2016-11-17 16:15:35 +00:00
|
|
|
return this._id.toHexString();
|
2016-08-09 22:45:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
userSchema.set('toJSON', {
|
2016-11-17 16:15:35 +00:00
|
|
|
virtuals: true
|
2016-08-09 22:45:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-05-17 19:50:37 +00:00
|
|
|
/**
|
|
|
|
* Helper method for validating user's password.
|
|
|
|
*/
|
2016-06-27 17:09:18 +00:00
|
|
|
userSchema.methods.comparePassword = function comparePassword(candidatePassword, cb) {
|
2016-06-24 22:18:22 +00:00
|
|
|
// userSchema.methods.comparePassword = (candidatePassword, cb) => {
|
2016-05-17 19:50:37 +00:00
|
|
|
bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
|
|
|
|
cb(err, isMatch);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-01-06 23:14:42 +00:00
|
|
|
userSchema.statics.findByMailOrName = function findByMailOrName(email) {
|
|
|
|
const query = {
|
|
|
|
$or: [{
|
|
|
|
email,
|
|
|
|
}, {
|
|
|
|
username: email,
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
return this.findOne(query).exec();
|
|
|
|
};
|
|
|
|
|
2017-06-26 16:48:28 +00:00
|
|
|
userSchema.statics.EmailConfirmation = EmailConfirmationStates;
|
|
|
|
|
2016-06-24 22:18:22 +00:00
|
|
|
export default mongoose.model('User', userSchema);
|