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 } export const UserModel = mongoose.model("user", UserSchema) export interface User { username: string; password: string; } export async function encryptPwd(pwd: string) { return Promise.resolve(bcrypt.hash(pwd, 10)) } export async function pwdIsValid(pwd: string, user: User): Promise { return Promise.resolve(bcrypt.compare(pwd, user.password)) }