diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index f5c1c92..e77964b 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -40,7 +40,6 @@ export class AuthController { value: { operador: '', password: '', - id_institucion: 200, id_modulo: 1, }, }, @@ -48,7 +47,6 @@ export class AuthController { }) loginOperador(@Body() body: AuthLoginOperadorDto) { return this.authService.loginOperador( - body.id_institucion, body.id_modulo, body.operador, body.password, @@ -56,7 +54,9 @@ export class AuthController { } @Post('login-usuario') - @ApiOperation({ description: 'Endpoint utilizado para el login del usuario.' }) + @ApiOperation({ + description: 'Endpoint utilizado para el login del usuario.', + }) @ApiBody({ description: 'Todas las variables son obligatorias.', examples: { ejemplo: { value: { usuario: '', password: '' } } }, diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index adf2167..4d504c1 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -4,6 +4,8 @@ import { UnauthorizedException, } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; +import { Operador } from 'src/operador/entity/operador.entity'; +import { Usuario } from 'src/usuario/entity/usuario.entity'; import { BcryptService } from '../bcrypt/bcrypt.service'; import { ModuloService } from '../modulo/modulo.service'; import { OperadorService } from '../operador/operador.service'; @@ -20,21 +22,9 @@ export class AuthService { private usuarioService: UsuarioService, ) {} - validate() {} - loginAdmin(admin: string, password: string) { return this.operadorService.findAdmin(admin, false).then((operador) => { - if ( - !operador || - !this.bcryptService.comparar(password, operador.password) - ) - throw new UnauthorizedException( - 'Usuario y/o password incorrectos, trata de nuevo.', - ); - if (!operador.activo) - throw new UnauthorizedException( - 'Esta cuenta se encuentra desactivada.', - ); + this.validarLogin(operador, password); const payload: JwtPayload = { id_operador: operador.id_operador, @@ -45,35 +35,13 @@ export class AuthService { }); } - async loginOperador( - id_institucion: number, - id_modulo: number, - operador: string, - password: string, - ) { + async loginOperador(id_modulo: number, operador: string, password: string) { const modulo = await this.moduloService.findById(id_modulo); return this.operadorService - .findByOperador(id_institucion, operador, false) + .findByOperador(modulo.institucion, operador, false) .then((operador) => { - if ( - !operador || - !this.bcryptService.comparar(password, operador.password) - ) - throw new UnauthorizedException( - 'Usuario y/o password incorrectos, trata de nuevo.', - ); - if (!operador.activo) - throw new UnauthorizedException( - 'Esta cuenta se encuentra desactivada.', - ); - if ( - modulo.institucion.id_institucion != - operador.institucion.id_institucion - ) - throw new ConflictException( - 'El módulo seleccionado no pertenece a la misma institución al la que pertenece el operador.', - ); + this.validarLogin(operador, password); const payload: JwtPayload = { id_operador: operador.id_operador, @@ -87,10 +55,7 @@ export class AuthService { loginUsuario(usuario: string, password: string) { return this.usuarioService.findByUsuario(usuario, false).then((usuario) => { - if (!usuario || !this.bcryptService.comparar(password, usuario.password)) - throw new UnauthorizedException( - 'Usuario y/o password incorrectos, trata de nuevo.', - ); + this.validarLogin(usuario, password); const payload: JwtPayload = { id_usuario: usuario.id_usuario, @@ -100,4 +65,16 @@ export class AuthService { return { usuario, token: this.jwtService.sign(payload) }; }); } + + validarLogin(usuarioOperador: Usuario | Operador, password: string) { + if ( + !usuarioOperador || + !this.bcryptService.comparar(password, usuarioOperador.password) + ) + throw new UnauthorizedException( + 'Usuario y/o password incorrectos, trata de nuevo.', + ); + if (!usuarioOperador.activo) + throw new ConflictException('Esta cuenta se encuentra desactivada.'); + } } diff --git a/src/auth/dto/auth-login-operador.dto.ts b/src/auth/dto/auth-login-operador.dto.ts index c7c5ebd..9757dbf 100644 --- a/src/auth/dto/auth-login-operador.dto.ts +++ b/src/auth/dto/auth-login-operador.dto.ts @@ -1,9 +1,6 @@ import { IsInt, IsString } from 'class-validator'; export class AuthLoginOperadorDto { - @IsInt() - id_institucion: number; - @IsInt() id_modulo: number; diff --git a/src/auth/dto/auth-login-usuario.dto.ts b/src/auth/dto/auth-login-usuario.dto.ts index cdaaeea..130976e 100644 --- a/src/auth/dto/auth-login-usuario.dto.ts +++ b/src/auth/dto/auth-login-usuario.dto.ts @@ -1,7 +1,7 @@ -import { IsNumberString, IsString } from 'class-validator'; +import { IsString } from 'class-validator'; export class AuthLoginUsuarioDto { - @IsNumberString() + @IsString() usuario: string; @IsString()