|
1234567891011121314151617181920212223242526272829 |
- 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<string>;
-
- assertEqual(password: string, hashedPassword: string): Promise<void>;
- }
-
- export class PasswordServiceImpl implements PasswordService {
- constructor(private readonly saltRounds = 12) {
- }
-
- async hash(password: string): Promise<string> {
- const salt = await genSalt(this.saltRounds)
- return hash(password, salt)
- }
-
- async assertEqual(password: string, hashedPassword: string): Promise<void> {
- const result = await compare(password, hashedPassword)
- if (result) {
- return
- }
- throw new PasswordNotEqualAssertError()
- }
- }
|