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!'); } }