diff --git a/src/bcrypt/bcrypt.module.ts b/src/bcrypt/bcrypt.module.ts index 937f55c..08af691 100644 --- a/src/bcrypt/bcrypt.module.ts +++ b/src/bcrypt/bcrypt.module.ts @@ -1,8 +1,5 @@ import { Module } from '@nestjs/common'; import { BcryptService } from './bcrypt.service'; -@Module({ - providers: [BcryptService], - exports: [BcryptService], -}) +@Module({ providers: [BcryptService], exports: [BcryptService] }) export class BcryptModule {} diff --git a/src/bcrypt/bcrypt.service.ts b/src/bcrypt/bcrypt.service.ts index 4ebbb38..5f6bf9c 100644 --- a/src/bcrypt/bcrypt.service.ts +++ b/src/bcrypt/bcrypt.service.ts @@ -4,27 +4,32 @@ import { ConfigService } from '@nestjs/config'; @Injectable() export class BcryptService { - constructor(private configService: ConfigService) {} + private salt: string; + private passwordLength: number; - comparar(password: string, passwordDb: string) { + constructor(private configService: ConfigService) { + this.salt = bcrypt.genSaltSync( + parseInt(this.configService.get('SALT_ROUNDS')), + ); + this.passwordLength = parseInt( + this.configService.get('PASSWORD_LENGTH'), + ); + } + + comparar(password: string, passwordDb: string): boolean { return bcrypt.compareSync(password, passwordDb); } - encriptar(password: string) { - const salt = bcrypt.genSaltSync( - parseInt(this.configService.get('SALT_ROUNDS')), - ); - - return bcrypt.hashSync(password, salt); + encriptar(password: string): string { + return bcrypt.hashSync(password, this.salt); } - generarPassword = () => { - const length = 10; + generarPassword = (): string => { const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-@#$%&=?¿*()/_-'; let password = ''; - for (let i = 0; i < length; i++) + for (let i = 0; i < this.passwordLength; i++) password += charset.charAt(Math.floor(Math.random() * charset.length)); return password; };