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
|
|
|
|
2018-10-14 19:08:36 +00:00
|
|
|
const apiKeySchema = new Schema({
|
|
|
|
label: { type: String, default: 'API Key' },
|
2019-05-15 10:28:18 +00:00
|
|
|
lastUsedAt: { type: Date },
|
2018-10-14 19:08:36 +00:00
|
|
|
hashedKey: { type: String, required: true },
|
|
|
|
}, { timestamps: true, _id: true });
|
|
|
|
|
2019-05-15 11:07:20 +00:00
|
|
|
apiKeySchema.virtual('id').get(function getApiKeyId() {
|
|
|
|
return this._id.toHexString();
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When serialising an APIKey instance, the `hashedKey` field
|
|
|
|
* should never be exposed to the client. So we only return
|
|
|
|
* a safe list of fields when toObject and toJSON are called.
|
|
|
|
*/
|
|
|
|
function apiKeyMetadata(doc, ret, options) {
|
2019-05-14 17:50:33 +00:00
|
|
|
return {
|
2019-05-15 11:07:20 +00:00
|
|
|
id: doc.id, label: doc.label, lastUsedAt: doc.lastUsedAt, createdAt: doc.createdAt
|
2019-05-14 17:50:33 +00:00
|
|
|
};
|
2019-05-15 11:07:20 +00:00
|
|
|
}
|
2019-05-14 10:26:25 +00:00
|
|
|
|
2019-05-15 11:07:20 +00:00
|
|
|
apiKeySchema.set('toObject', {
|
|
|
|
transform: apiKeyMetadata
|
2018-10-14 19:08:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
apiKeySchema.set('toJSON', {
|
2019-05-15 11:07:20 +00:00
|
|
|
virtuals: true,
|
|
|
|
transform: apiKeyMetadata
|
2018-10-14 19:08:36 +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 },
|
2021-01-27 21:14:21 +00:00
|
|
|
email: { type: String },
|
2016-06-23 22:29:55 +00:00
|
|
|
tokens: Array,
|
2018-10-14 19:08:36 +00:00
|
|
|
apiKeys: { type: [apiKeySchema] },
|
2016-08-04 03:45:49 +00:00
|
|
|
preferences: {
|
2016-08-09 22:45:59 +00:00
|
|
|
fontSize: { type: Number, default: 18 },
|
2019-08-30 16:36:34 +00:00
|
|
|
lineNumbers: { type: Boolean, default: true },
|
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' },
|
2020-08-17 09:23:58 +00:00
|
|
|
autorefresh: { type: Boolean, default: false },
|
|
|
|
language: { type: String, default: 'en-US' }
|
2019-08-08 21:34:49 +00:00
|
|
|
},
|
|
|
|
totalSize: { type: Number, default: 0 }
|
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
|
|
|
});
|
|
|
|
|
2018-11-06 12:36:19 +00:00
|
|
|
/**
|
|
|
|
* API keys hash middleware
|
|
|
|
*/
|
2018-11-06 16:28:17 +00:00
|
|
|
userSchema.pre('save', function checkApiKey(next) { // eslint-disable-line consistent-return
|
2018-11-06 12:36:19 +00:00
|
|
|
const user = this;
|
|
|
|
if (!user.isModified('apiKeys')) { return next(); }
|
|
|
|
let hasNew = false;
|
|
|
|
user.apiKeys.forEach((k) => {
|
|
|
|
if (k.isNew) {
|
|
|
|
hasNew = true;
|
2018-11-06 16:28:17 +00:00
|
|
|
bcrypt.genSalt(10, (err, salt) => { // eslint-disable-line consistent-return
|
2018-11-06 12:36:19 +00:00
|
|
|
if (err) { return next(err); }
|
|
|
|
bcrypt.hash(k.hashedKey, salt, null, (innerErr, hash) => {
|
|
|
|
if (innerErr) { return next(innerErr); }
|
|
|
|
k.hashedKey = hash;
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!hasNew) return next();
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-11-06 16:28:17 +00:00
|
|
|
/**
|
|
|
|
* Helper method for validating a user's api key
|
|
|
|
*/
|
|
|
|
userSchema.methods.findMatchingKey = function findMatchingKey(candidateKey, cb) {
|
|
|
|
let foundOne = false;
|
|
|
|
this.apiKeys.forEach((k) => {
|
|
|
|
if (bcrypt.compareSync(candidateKey, k.hashedKey)) {
|
|
|
|
foundOne = true;
|
2019-05-15 12:07:46 +00:00
|
|
|
cb(null, true, k);
|
2018-11-06 16:28:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!foundOne) cb('Matching API key not found !', false, null);
|
|
|
|
};
|
|
|
|
|
2020-07-15 21:33:11 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Queries User collection by email and returns one User document.
|
|
|
|
*
|
|
|
|
* @param {string|string[]} email - Email string or array of email strings
|
|
|
|
* @callback [cb] - Optional error-first callback that passes User document
|
|
|
|
* @return {Promise<Object>} - Returns Promise fulfilled by User document
|
|
|
|
*/
|
|
|
|
userSchema.statics.findByEmail = function findByEmail(email, cb) {
|
|
|
|
let query;
|
|
|
|
if (Array.isArray(email)) {
|
|
|
|
query = {
|
|
|
|
email: { $in: email }
|
2020-07-14 22:16:17 +00:00
|
|
|
};
|
2020-07-15 21:33:11 +00:00
|
|
|
} else {
|
|
|
|
query = {
|
|
|
|
email
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// Email addresses should be case-insensitive unique
|
|
|
|
// In MongoDB, you must use collation in order to do a case-insensitive query
|
|
|
|
return this.findOne(query).collation({ locale: 'en', strength: 2 }).exec(cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Queries User collection by username and returns one User document.
|
|
|
|
*
|
|
|
|
* @param {string} username - Username string
|
|
|
|
* @callback [cb] - Optional error-first callback that passes User document
|
|
|
|
* @return {Promise<Object>} - Returns Promise fulfilled by User document
|
|
|
|
*/
|
|
|
|
userSchema.statics.findByUsername = function findByUsername(username, cb) {
|
|
|
|
const query = {
|
|
|
|
username
|
|
|
|
};
|
|
|
|
return this.findOne(query, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Queries User collection using email or username with optional callback.
|
|
|
|
* This function will determine automatically whether the data passed is
|
|
|
|
* a username or email.
|
|
|
|
*
|
|
|
|
* @param {string} value - Email or username
|
|
|
|
* @callback [cb] - Optional error-first callback that passes User document
|
|
|
|
* @return {Promise<Object>} - Returns Promise fulfilled by User document
|
|
|
|
*/
|
|
|
|
userSchema.statics.findByEmailOrUsername = function findByEmailOrUsername(value, cb) {
|
2021-01-27 21:14:21 +00:00
|
|
|
// const isEmail = value.indexOf('@') > -1;
|
|
|
|
// if (isEmail) {
|
|
|
|
// return this.findByEmail(value, cb);
|
|
|
|
// }
|
2020-07-15 21:33:11 +00:00
|
|
|
return this.findByUsername(value, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Queries User collection, performing a MongoDB logical or with the email
|
|
|
|
* and username (i.e. if either one matches, will return the first document).
|
|
|
|
*
|
|
|
|
* @param {string} email
|
|
|
|
* @param {string} username
|
|
|
|
* @callback [cb] - Optional error-first callback that passes User document
|
|
|
|
* @return {Promise<Object>} - Returns Promise fulfilled by User document
|
|
|
|
*/
|
|
|
|
userSchema.statics.findByEmailAndUsername = function findByEmailAndUsername(email, username, cb) {
|
2021-01-27 21:14:21 +00:00
|
|
|
const query = { username // override username only as we don't want to register email addresses
|
|
|
|
// $or: [
|
|
|
|
// { email },
|
|
|
|
// { username }
|
|
|
|
// ]
|
2017-01-06 23:14:42 +00:00
|
|
|
};
|
2020-07-15 21:33:11 +00:00
|
|
|
return this.findOne(query).collation({ locale: 'en', strength: 2 }).exec(cb);
|
2017-01-06 23:14:42 +00:00
|
|
|
};
|
|
|
|
|
2017-06-26 16:48:28 +00:00
|
|
|
userSchema.statics.EmailConfirmation = EmailConfirmationStates;
|
|
|
|
|
2020-07-14 22:16:17 +00:00
|
|
|
userSchema.index({ username: 1 }, { collation: { locale: 'en', strength: 2 } });
|
|
|
|
userSchema.index({ email: 1 }, { collation: { locale: 'en', strength: 2 } });
|
|
|
|
|
2016-06-24 22:18:22 +00:00
|
|
|
export default mongoose.model('User', userSchema);
|