import { hash, genSalt, compare } from 'bcrypt' import { AppError } from 'src/packages/fastify-compliant-http-errors' export class PasswordNotEqualAssertError extends AppError { } export interface PasswordService { hash(password: string): Promise; assertEqual(password: string, hashedPassword: string): Promise; } export class PasswordServiceImpl implements PasswordService { constructor(private readonly saltRounds = 12) { } async hash(password: string): Promise { const salt = await genSalt(this.saltRounds) return hash(password, salt) } async assertEqual(password: string, hashedPassword: string): Promise { const result = await compare(password, hashedPassword) if (result) { return } throw new PasswordNotEqualAssertError() } }