Skip to content

arthur-rs/nestjs-hash

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

Hashing library for NestJS.

Installation

$ npm install nestjs-hash

or

$ yarn add nestjs-hash

Hou to use

1 - first add hash module in import for your module

Using with native hash

  import { HashModule } from 'nestjs-hash';

  @Module({
    imports: [HashModule.forRoot({ type: 'sha256' })],
  })
  export default UsersModule;

or external library hash

  import { HashModule } from 'nestjs-hash';

  @Module({
    imports: [HashModule.forRoot({ type: 'bcrypt', rounds: 16 })],
  })
  export default UsersModule;

Attention to use bcrypt or more libraries you need to add them as dependencies to your project.

  • bcrypt
$ npm install bcrypt
  • argon2
$ npm install argon2

2 - second uses dependency injection to add the hash service to your service

  @Injectable()
  export class UsersService() {
    constructor(
      private readonly hashService: HashService,
      private readonly usersRepository: UsersRepository
    ) {}

    public async createUser(data) {
      const passwordHash = await this.hashService.hash(data.password);
      const user = await this.usersRepository.create({
        ...data,
        password: passwordHash
      });
      return user;
    }
  };

Author

Created with more love by Arthur Reis

License

MIT licensed.