Untitled
unknown
plain_text
a year ago
3.6 kB
7
Indexable
// Import required modules const crypto = require('crypto'); const mongoose = require('mongoose'); const validator = require('validator'); const bcrypt = require('bcryptjs'); // Define user schema const userSchema = new mongoose.Schema({ name: { type: String, required: [true, 'Please, tell us your name!'], unique: true }, email: { type: String, required: [true, 'Please provide your email!'], unique: true, lowercase: true, validate: [validator.default.isEmail, 'Please provide a valid email address!'] }, phone: { type: String, required: [true, 'Please type your phone!'] }, password: { type: String, required: [true, 'Please provide a password'], minlength: 8, select: false }, passwordConfirm: { type: String, required: [true, 'Please confirm your password'], validate: { validator: function(el) { return el === this.password; }, message: 'Passwords are not the same' } }, role: { type: String, enum: ['admin', 'partner', 'owner', 'agent'], required: true }, commissionDetails: { commission: { type: Number, required: function() { return this.role === 'agent'; } }, currency: { type: String, required: function() { return this.role === 'agent'; } }, target: { type: Number, required: function() { return this.role === 'agent'; } }, invoiceTime: { type: String, required: function() { return this.role === 'agent'; } } }, passwordChangedAt: Date, passwordResetToken: String, passwordResetExpires: Date, profileImage: { type: String, default: 'default.jpg' }, active: { type: Boolean, default: false, select: false } }, { toJSON: { virtuals: true }, toObject: { virtuals: true } }); // Middleware to hash password before saving to database userSchema.pre('save', async function(next) { if (!this.isModified('password')) return next(); this.password = await bcrypt.hash(this.password, 12); this.passwordConfirm = undefined; next(); }); // Middleware to update passwordChangedAt field when password is changed userSchema.pre('save', function(next) { if (!this.isModified('password') || this.isNew) return next(); this.passwordChangedAt = Date.now() - 1000; next(); }); // Middleware to eliminate inactive users from find queries userSchema.pre(/^find/, function(next) { this.find({ active: { $ne: true } }); next(); }); // Method to verify if passwords match userSchema.methods.correctPassword = async function(candidatePassword, userPassword) { return await bcrypt.compare(candidatePassword, userPassword); }; // Method to check if password was changed after token was issued userSchema.methods.changedPasswordAfter = function(JWTTimestamp) { if (this.passwordChangedAt) { const changedTimeStamp = parseInt(this.passwordChangedAt.getTime() / 1000, 10); return changedTimeStamp > JWTTimestamp; } return false; // False means NOT changed }; // Method to create password reset token userSchema.methods.createPasswordResetToken = function() { const resetToken = crypto.randomBytes(32).toString('hex'); this.passwordResetToken = crypto.createHash('sha256').update(resetToken).digest('hex'); this.passwordResetExpires = Date.now() + 10 * 60 * 1000; return resetToken; }; // Create User model from schema const User = mongoose.model('User', userSchema); // Export User model module.exports = User;
Editor is loading...
Leave a Comment