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:
parent
2aa793c91d
commit
cac7d4cfd3
10
src/credentials/credentials.module.ts
Normal file
10
src/credentials/credentials.module.ts
Normal 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 {}
|
24
src/credentials/credentials.service.ts
Normal file
24
src/credentials/credentials.service.ts
Normal 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"),
|
||||
})
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user