mirror of
https://github.com/R0m1k3/podcastic.git
synced 2026-10-19 21:18:03 +02:00
90 lines
1.8 KiB
TypeScript
90 lines
1.8 KiB
TypeScript
import mongoose, { Schema, Document } from 'mongoose';
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
export interface IUser extends Document {
|
|
email: string;
|
|
password: string;
|
|
username: string;
|
|
avatar?: string;
|
|
preferences: {
|
|
defaultPlaybackSpeed: number;
|
|
theme: 'light' | 'dark';
|
|
language: string;
|
|
};
|
|
createdAt: Date;
|
|
updatedAt: Date;
|
|
comparePassword(password: string): Promise<boolean>;
|
|
}
|
|
|
|
const userSchema = new Schema<IUser>(
|
|
{
|
|
email: {
|
|
type: String,
|
|
required: true,
|
|
unique: true,
|
|
lowercase: true,
|
|
trim: true,
|
|
},
|
|
password: {
|
|
type: String,
|
|
required: true,
|
|
minlength: 6,
|
|
},
|
|
username: {
|
|
type: String,
|
|
required: true,
|
|
trim: true,
|
|
},
|
|
avatar: {
|
|
type: String,
|
|
default: null,
|
|
},
|
|
preferences: {
|
|
defaultPlaybackSpeed: {
|
|
type: Number,
|
|
default: 1,
|
|
enum: [0.75, 1, 1.25, 1.5, 1.75, 2],
|
|
},
|
|
theme: {
|
|
type: String,
|
|
default: 'light',
|
|
enum: ['light', 'dark'],
|
|
},
|
|
language: {
|
|
type: String,
|
|
default: 'en',
|
|
},
|
|
},
|
|
},
|
|
{
|
|
timestamps: true,
|
|
}
|
|
);
|
|
|
|
// Hash password before saving
|
|
userSchema.pre('save', async function (next) {
|
|
if (!this.isModified('password')) return next();
|
|
|
|
try {
|
|
const salt = await bcrypt.genSalt(10);
|
|
this.password = await bcrypt.hash(this.password, salt);
|
|
next();
|
|
} catch (error) {
|
|
next(error as Error);
|
|
}
|
|
});
|
|
|
|
// Method to compare passwords
|
|
userSchema.methods.comparePassword = async function (password: string): Promise<boolean> {
|
|
return bcrypt.compare(password, this.password);
|
|
};
|
|
|
|
// Remove password from JSON output
|
|
userSchema.methods.toJSON = function () {
|
|
const obj = this.toObject();
|
|
delete obj.password;
|
|
return obj;
|
|
};
|
|
|
|
export const User = mongoose.model<IUser>('User', userSchema);
|