mirror of
https://github.com/philipredstone/relnet.git
synced 2025-06-17 13:11:14 +02:00
55 lines
1.2 KiB
TypeScript
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);
|