1.3 KiB

Password Hasher

This Service is for NestJs and can create a Hash of a Password and verify it.

Install

npm i --save @apihub24/password-hasher

Dependencies

Dependency Description
@nestjs/config use the NestJs Config Module to get the Password Salt Rounds with key PASSWORD_HASH_SALT_ROUNDS

Usage

// register the PasswordHashService as Provider in a NestJs Module
import { PasswordHashService } from '@apihub24/password-hasher';

@Module({
  // import the ConfigModule! not forget to set the PASSWORD_HASH_SALT_ROUNDS in Config otherwise 10 was used
  imports: [ConfigModule.forRoot()]
  providers: [PasswordHashService],
})
...

// inject the PasswordHashService
export class SomeOtherService {
  constructor(@Inject(PasswordHashService) passwordHashService: PasswordHashService) {}

  async test(password: string) {
    // generate the hash
    const hash = await this.passwordHashService(password);

    // compare a plaintext Password with Hash
    await this.passwordHashService.verify(password, hash);
  }
}