relnet/server/models/user.model.ts
2025-04-17 13:06:50 +02:00

55 lines
1.2 KiB
TypeScript

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<boolean>;
}
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<boolean> {
return bcrypt.compare(candidatePassword, this.password);
};
export default mongoose.model<IUser>('User', UserSchema);