diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index ee2e6b2..d854d28 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -1,25 +1,31 @@ import { Body, Controller, Post } from '@nestjs/common'; import { AuthService } from './auth.service'; +import { AuthLoginAdminDto } from './dto/auth-login-admin.dto'; import { AuthLoginOperadorDto } from './dto/auth-login-operador.dto'; import { AuthLoginUsuarioDto } from './dto/auth-login-usuario.dto'; -import {ApiTags} from '@nestjs/swagger' +import { ApiTags } from '@nestjs/swagger'; @Controller('auth') @ApiTags('auth') export class AuthController { constructor(private authService: AuthService) {} - @Post('login_operador') + @Post('login-admin') + loginAdmin(@Body() body: AuthLoginAdminDto) { + return this.authService.loginAdmin(body.operador, body.password); + } + + @Post('login-operador') loginOperador(@Body() body: AuthLoginOperadorDto) { return this.authService.loginOperador( - Number(body.id_institucion), - Number(body.id_modulo), + body.id_institucion, + body.id_modulo, body.operador, body.password, ); } - @Post('login_usuario') + @Post('login-usuario') loginUsuario(@Body() body: AuthLoginUsuarioDto) { return this.authService.loginUsuario(body.usuario, body.password); } diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 336f12b..d0fa199 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -22,19 +22,23 @@ export class AuthService { validate() {} - loginUsuario(usuario: string, password: string) { - return this.usuarioService.findByUsuario(usuario, false).then((usuario) => { - if (!usuario || !this.bcryptService.comparar(password, usuario.password)) + loginAdmin(admin: string, password: string) { + return this.operadorService.findByAdmin(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.', + ); const payload: JwtPayload = { - id_usuario: usuario.id_usuario, - id_tipo_usuario: usuario.tipoUsuario.id_tipo_usuario, + id_operador: operador.id_operador, + id_tipo_usuario: operador.tipoUsuario.id_tipo_usuario, }; - return { usuario, token: this.jwtService.sign(payload) }; + return { operador, token: this.jwtService.sign(payload) }; }); } @@ -47,9 +51,12 @@ export class AuthService { const modulo = await this.moduloService.findById(id_modulo); return this.operadorService - .findByOperador(id_institucion, operador) + .findByOperador(operador, id_institucion) .then((operador) => { - if (!this.bcryptService.comparar(password, operador.password)) + if ( + !operador || + !this.bcryptService.comparar(password, operador.password) + ) throw new UnauthorizedException( 'Usuario y/o password incorrectos, trata de nuevo.', ); @@ -74,4 +81,20 @@ export class AuthService { return { operador, token: this.jwtService.sign(payload) }; }); } + + 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.', + ); + + const payload: JwtPayload = { + id_usuario: usuario.id_usuario, + id_tipo_usuario: usuario.tipoUsuario.id_tipo_usuario, + }; + + return { usuario, token: this.jwtService.sign(payload) }; + }); + } } diff --git a/src/auth/dto/auth-login-admin.dto.ts b/src/auth/dto/auth-login-admin.dto.ts new file mode 100644 index 0000000..66cc79b --- /dev/null +++ b/src/auth/dto/auth-login-admin.dto.ts @@ -0,0 +1,9 @@ +import { IsString } from 'class-validator'; + +export class AuthLoginAdminDto { + @IsString() + operador: string; + + @IsString() + password: string; +} diff --git a/src/auth/dto/auth-login-operador.dto.ts b/src/auth/dto/auth-login-operador.dto.ts index 7f523d0..c7c5ebd 100644 --- a/src/auth/dto/auth-login-operador.dto.ts +++ b/src/auth/dto/auth-login-operador.dto.ts @@ -2,10 +2,10 @@ import { IsInt, IsString } from 'class-validator'; export class AuthLoginOperadorDto { @IsInt() - id_institucion: string; + id_institucion: number; @IsInt() - id_modulo: string; + id_modulo: number; @IsString() operador: string; diff --git a/src/operador/operador.service.ts b/src/operador/operador.service.ts index 6172fe9..888ff9f 100644 --- a/src/operador/operador.service.ts +++ b/src/operador/operador.service.ts @@ -4,7 +4,7 @@ import { NotFoundException, } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; +import { Between, Repository } from 'typeorm'; import { Operador } from './entity/operador.entity'; import { Institucion } from '../institucion/entity/institucion.entity'; import { BcryptService } from '../bcrypt/bcrypt.service'; @@ -61,6 +61,21 @@ export class OperadorService { return this.repository.find(busqueda); } + findByAdmin(admin: string, noExisteValidar = true) { + return this.repository + .findOne({ + where: { + operador: admin, + tipoUsuario: { id_tipo_usuario: Between(2, 3) }, + }, + }) + .then((admin) => { + if (noExisteValidar && !admin) + throw new NotFoundException('No existe este admin.'); + return admin; + }); + } + findById(id_operador: number) { return this.repository.findOne({ id_operador }).then((operador) => { if (!operador) throw new NotFoundException('No existe este operador'); @@ -68,12 +83,17 @@ export class OperadorService { }); } - findByOperador(id_institucion: number, operador: string) { + findByOperador( + operador: string, + id_institucion: number, + noExisteValidar = true, + ) { return this.institucionService .findById(id_institucion) .then((institucion) => this.repository.findOne({ institucion, operador })) .then((operador) => { - if (!operador) throw new NotFoundException('No existe este operador.'); + if (noExisteValidar && !operador) + throw new NotFoundException('No existe este operador.'); return operador; }); }