feat(credentials): add new Credentials service and module

Introduce the Credentials service which is responsible for hashing and verifying passwords. Added appropriate methods within the service and provided it in a new Credentials module. The module exports the service to allow its use in other parts of the application.
This commit is contained in:
Mathis H (Avnyr) 2024-07-09 15:06:37 +02:00
parent 2aa793c91d
commit cac7d4cfd3
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
2 changed files with 34 additions and 0 deletions

View File

@ -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 {}

View File

@ -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"),
})
}
}