import mongoose, { Document, Schema } from 'mongoose'; import bcrypt from 'bcryptjs'; export interface IUser extends Document { _id: string; email: string; password: string; username: string; comparePassword(candidatePassword: string): Promise; } const UserSchema = new Schema( { email: { type: String, required: [true, 'Email is required'], unique: true, trim: true, lowercase: true, }, password: { type: String, required: [true, 'Password is required'], minlength: 6, }, username: { type: String, required: [true, 'Username is required'], trim: true, }, }, { timestamps: true } ); // Hash password before saving UserSchema.pre('save', async function (next) { const user = this; if (!user.isModified('password')) return next(); try { const salt = await bcrypt.genSalt(10); user.password = await bcrypt.hash(user.password, salt); next(); } catch (error: any) { next(error); } }); // Compare password method UserSchema.methods.comparePassword = async function (candidatePassword: string): Promise { return bcrypt.compare(candidatePassword, this.password); }; export default mongoose.model('User', UserSchema);