passport-jwt/model/model.mts

42 lines
719 B
TypeScript

import mongoose from "mongoose";
import bcrypt from "bcrypt"
const Schema = mongoose.Schema
const UserSchema = new Schema({
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
}
})
UserSchema.pre(
"save",
async function(next) {
const hash = await bcrypt.hash(this.password, 10)
this.password = hash;
next();
}
)
UserSchema.methods.isValidPassword = async function(password: string) {
const compare = await bcrypt.compare(password, this.password)
return compare
}
export interface User {
email: string;
password: string;
isValidPassword: (password: string) => Promise<boolean>
}
export const UserModel = mongoose.model("user", UserSchema)