diff --git a/src/credentials/credentials.module.ts b/src/credentials/credentials.module.ts new file mode 100644 index 0000000..d655932 --- /dev/null +++ b/src/credentials/credentials.module.ts @@ -0,0 +1,10 @@ +import { Module } from "@nestjs/common"; +import { CredentialsService } from "./credentials.service"; +import { ConfigModule } from "@nestjs/config"; + +@Module({ + imports: [ConfigModule], + providers: [CredentialsService], + exports: [CredentialsService] +}) +export class CredentialsModule {} diff --git a/src/credentials/credentials.service.ts b/src/credentials/credentials.service.ts new file mode 100644 index 0000000..6c25230 --- /dev/null +++ b/src/credentials/credentials.service.ts @@ -0,0 +1,24 @@ +import { BadRequestException, Injectable } from "@nestjs/common"; +import * as argon from "argon2"; +// biome-ignore lint/style/useImportType: used by Next.js +import { ConfigService } from "@nestjs/config"; + +@Injectable() +export class CredentialsService { + + constructor(private configService: ConfigService) { + } + + async hash(plaintextPassword: string) { + if (plaintextPassword.length < 6) throw new BadRequestException("Password is not strong enough !") + return argon.hash(Buffer.from(plaintextPassword), { + secret: this.configService.get("APP_HASH_SECRET"), + }) + } + + async check(plaintextPassword: string, hashedPassword: string) { + return argon.verify(hashedPassword, Buffer.from(plaintextPassword), { + secret: this.configService.get("APP_HASH_SECRET"), + }) + } +}