/* eslint-disable no-invalid-this */ /* eslint-disable func-names */ import mongoose from 'mongoose'; import uniqueValidator from 'mongoose-unique-validator'; import crypto from 'crypto'; const UserSchema = new mongoose.Schema( { username: { type: String, unique: true, required: [true, 'username-required'], match: [/^[a-zA-Z0-9]+$/, 'username-match'], index: true, }, email: { type: String, lowercase: true, unique: true, required: [true, 'email-required'], match: [/\S+@\S+\.\S+/, 'email-match'], index: true, }, hash: String, salt: String, }, {timestamps: true}, ); UserSchema.plugin(uniqueValidator, {message: 'email-dup'}); UserSchema.methods.validPassword = function(password) { const [salt, key] = this.hash.split(':'); return key === crypto.scryptSync(password, salt, 64).toString('hex'); }; UserSchema.pre('save', function(next) { const user = this; if (!user.isModified('salt')) { return next(); } const salt = crypto.randomBytes(16).toString('hex'); return crypto.scrypt(user.salt, salt, 64, (err, derivedKey) => { if (err) { next(err); } this.salt = salt; this.hash = `${salt}:${derivedKey.toString('hex')}`; next(); }); }); export default mongoose.model('Users', UserSchema);