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