From 59b1af8c6b1fb89067edb292e47c9e05e49c62c0 Mon Sep 17 00:00:00 2001 From: Henrique Melo Date: Fri, 20 Oct 2023 17:52:14 -0300 Subject: [PATCH] (#137) altera nomes e legibilidade --- .env.development | 6 ++++-- .env.test | 5 +++-- src/usuario/usuario.service.ts | 19 +++++++++---------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/.env.development b/.env.development index 9905d30..1d054b2 100644 --- a/.env.development +++ b/.env.development @@ -5,5 +5,7 @@ DB_USERNAME=postgres DB_PASS=postgres DB_DATABASE=gerocuidado-usuario-db DB_PORT=5001 -#SALTROUND -SALTROUND=10 + +#BCRYPT +HASH_SALT=10 + diff --git a/.env.test b/.env.test index bac5cd5..fe56cc4 100644 --- a/.env.test +++ b/.env.test @@ -5,5 +5,6 @@ DB_USERNAME=postgres DB_PASS=postgres DB_DATABASE=gerocuidado-usuario-db-test DB_PORT=5001 -#SALTROUND -SALTROUND=10 \ No newline at end of file + +#BCRYPT +HASH_SALT=10 diff --git a/src/usuario/usuario.service.ts b/src/usuario/usuario.service.ts index 4143451..9a34602 100644 --- a/src/usuario/usuario.service.ts +++ b/src/usuario/usuario.service.ts @@ -25,21 +25,20 @@ export class UsuarioService { async create(body: CreateUsuarioDto): Promise { const usuario = new Usuario(body); - await this.verificaEmailNoBanco(usuario.email); - usuario.senha = await this.criptografaSenha(usuario.senha); + await this.checkEmail(usuario.email); + usuario.senha = await this.hashPassword(usuario.senha); return this._repository.save(usuario); } - async criptografaSenha(senha: string): Promise { - const saltRounds = this._configService.get('SALTROUND'); - return bcrypt.hash(senha, Number(saltRounds)); + async hashPassword(senha: string): Promise { + const salt = this._configService.get('HASH_SALT'); + return bcrypt.hash(senha, Number(salt)); } - async verificaEmailNoBanco(email: string) { - const emailCadastrado = await this._repository.findOne({ - where: { email: email }, - }); - if (emailCadastrado) { + async checkEmail(email: string) { + const userFound = await this._repository.findOne({ where: { email } }); + + if (userFound) { throw new BadRequestException('Este email já está cadastrado!'); } }