From d38bd391d0503cd3073e925eb4be8c47edf59b4d Mon Sep 17 00:00:00 2001 From: xXpuma99Xx <51341582+xXpuma99Xx@users.noreply.github.com> Date: Thu, 21 Apr 2022 20:33:05 -0500 Subject: [PATCH] logins en auth --- src/auth/auth.controller.ts | 19 +++++++++-- src/auth/auth.module.ts | 6 ++++ src/auth/auth.service.ts | 33 +++++++++++++++++-- .../dto/auth-login-operador.dto.ts} | 2 +- .../dto/auth-login-usuario.dto.ts} | 2 +- src/operador/operador.controller.ts | 6 ---- src/operador/operador.module.ts | 1 + src/operador/operador.service.ts | 12 ------- src/usuario/usuario.controller.ts | 6 ---- src/usuario/usuario.module.ts | 1 + src/usuario/usuario.service.ts | 12 ------- 11 files changed, 58 insertions(+), 42 deletions(-) rename src/{operador/dto/operador-login.dto.ts => auth/dto/auth-login-operador.dto.ts} (79%) rename src/{usuario/dto/usuario-login.dto.ts => auth/dto/auth-login-usuario.dto.ts} (79%) diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 268eeb2..09aec7a 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -1,4 +1,19 @@ -import { Controller } from '@nestjs/common'; +import { Body, Controller, Post } from '@nestjs/common'; +import { AuthService } from './auth.service'; +import { AuthLoginOperadorDto } from './dto/auth-login-operador.dto'; +import { AuthLoginUsuarioDto } from './dto/auth-login-usuario.dto'; @Controller('auth') -export class AuthController {} +export class AuthController { + constructor(private authService: AuthService) {} + + @Post('login_operador') + loginOperador(@Body() body: AuthLoginOperadorDto) { + return this.authService.loginOperador(body.operador, body.password); + } + + @Post('login_usuario') + loginUsuario(@Body() body: AuthLoginUsuarioDto) { + return this.authService.loginUsuario(body.usuario, body.password); + } +} diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index 9a53079..94020ab 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -4,7 +4,10 @@ import { JwtModule } from '@nestjs/jwt'; import { PassportModule } from '@nestjs/passport'; import { AuthController } from './auth.controller'; import { AuthService } from './auth.service'; +import { BcryptModule } from '../bcrypt/bcrypt.module'; import { JwtStrategyService } from './strategy/jwt-strategy.service'; +import { OperadorModule } from '../operador/operador.module'; +import { UsuarioModule } from '../usuario/usuario.module'; @Module({ imports: [ @@ -18,6 +21,9 @@ import { JwtStrategyService } from './strategy/jwt-strategy.service'; }; }, }), + BcryptModule, + OperadorModule, + UsuarioModule, ], controllers: [AuthController], providers: [AuthService, JwtStrategyService], diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 7b5de16..a7d4733 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -1,9 +1,38 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, UnauthorizedException } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; +import { BcryptService } from '../bcrypt/bcrypt.service'; +import { OperadorService } from '../operador/operador.service'; +import { UsuarioService } from '../usuario/usuario.service'; @Injectable() export class AuthService { - constructor() {} + constructor( + private bcryptService: BcryptService, + private operadorService: OperadorService, + private usuarioService: UsuarioService, + ) {} validate() {} + + loginUsuario(usuario: string, password: string) { + return this.usuarioService.findByUsuario(usuario).then((usuario) => { + if (!this.bcryptService.comparar(password, usuario.password)) + throw new UnauthorizedException( + 'Usuario y/o contraseña incorrectos, trata de nuevo.', + ); + /* Crear JWT y regresarlo */ + return usuario; + }); + } + + loginOperador(operador: string, password: string) { + return this.operadorService.findByOperador(operador).then((operador) => { + if (!this.bcryptService.comparar(password, operador.password)) + throw new UnauthorizedException( + 'Usuario y/o contraseña incorrectos, trata de nuevo.', + ); + /* Crear JWT y regresarlo */ + return operador; + }); + } } diff --git a/src/operador/dto/operador-login.dto.ts b/src/auth/dto/auth-login-operador.dto.ts similarity index 79% rename from src/operador/dto/operador-login.dto.ts rename to src/auth/dto/auth-login-operador.dto.ts index 9cc20e3..b1123e2 100644 --- a/src/operador/dto/operador-login.dto.ts +++ b/src/auth/dto/auth-login-operador.dto.ts @@ -1,6 +1,6 @@ import { IsNumberString, IsString } from 'class-validator'; -export class OperadorLoginDto { +export class AuthLoginOperadorDto { @IsNumberString() operador: string; diff --git a/src/usuario/dto/usuario-login.dto.ts b/src/auth/dto/auth-login-usuario.dto.ts similarity index 79% rename from src/usuario/dto/usuario-login.dto.ts rename to src/auth/dto/auth-login-usuario.dto.ts index 2168ad1..cdaaeea 100644 --- a/src/usuario/dto/usuario-login.dto.ts +++ b/src/auth/dto/auth-login-usuario.dto.ts @@ -1,6 +1,6 @@ import { IsNumberString, IsString } from 'class-validator'; -export class UsuarioLoginDto { +export class AuthLoginUsuarioDto { @IsNumberString() usuario: string; diff --git a/src/operador/operador.controller.ts b/src/operador/operador.controller.ts index faf7ac6..aaee20d 100644 --- a/src/operador/operador.controller.ts +++ b/src/operador/operador.controller.ts @@ -1,7 +1,6 @@ import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common'; import { OperadorService } from './operador.service'; import { OperadorCreateDto } from './dto/operador-create.dto'; -import { OperadorLoginDto } from './dto/operador-login.dto'; import { OperadorOperadoresDto } from './dto/operador-operadores.dto'; import { OperadorUpdateDto } from './dto/operador-update.dto'; import { OperadorDto } from './dto/operador.dto'; @@ -18,11 +17,6 @@ export class OperadorController { ); } - @Post('login') - login(@Body() body: OperadorLoginDto) { - return this.operadorService.login(body.operador, body.password); - } - @Get('operador') operador(@Query() query: OperadorDto) { return this.operadorService.findByOperador(query.operador); diff --git a/src/operador/operador.module.ts b/src/operador/operador.module.ts index 85ed3c6..2c5cd76 100644 --- a/src/operador/operador.module.ts +++ b/src/operador/operador.module.ts @@ -16,5 +16,6 @@ import { TipoUsuarioModule } from '../tipo-usuario/tipo-usuario.module'; ], controllers: [OperadorController], providers: [OperadorService], + exports: [OperadorService], }) export class OperadorModule {} diff --git a/src/operador/operador.service.ts b/src/operador/operador.service.ts index 4bff690..cbd78c9 100644 --- a/src/operador/operador.service.ts +++ b/src/operador/operador.service.ts @@ -2,7 +2,6 @@ import { ConflictException, Injectable, NotFoundException, - UnauthorizedException, } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; @@ -76,17 +75,6 @@ export class OperadorService { }); } - login(operador: string, password: string) { - return this.findByOperador(operador).then((operador) => { - if (!this.bcryptService.comparar(password, operador.password)) - throw new UnauthorizedException( - 'Usuario y/o contraseña incorrectos, trata de nuevo.', - ); - /* Crear JWT y regresarlo */ - return operador; - }); - } - update(attrs: Partial) { return this.findById(attrs.id_operador) .then((operador) => { diff --git a/src/usuario/usuario.controller.ts b/src/usuario/usuario.controller.ts index b688282..a1448dc 100644 --- a/src/usuario/usuario.controller.ts +++ b/src/usuario/usuario.controller.ts @@ -1,7 +1,6 @@ import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common'; import { UsuarioService } from './usuario.service'; import { UsuarioEscolaresDto } from './dto/usuario-escolares.dto'; -import { UsuarioLoginDto } from './dto/usuario-login.dto'; import { UsuarioRegistrarDto } from './dto/usuario-registrar.dto'; import { UsuarioUpdateDto } from './dto/usuario-update.dto'; import { UsuarioUsuariosDto } from './dto/usuario-usuarios.dto'; @@ -15,11 +14,6 @@ export class UsuarioController { @Get('escolares') escolares(@Query() query: UsuarioEscolaresDto) {} - @Post('login') - login(@Body() body: UsuarioLoginDto) { - return this.usuarioService.login(body.usuario, body.password); - } - @Post('registrar') registrar(@Body() body: UsuarioRegistrarDto) { return this.usuarioService.registrar(body.id_usuario, body.telefono); diff --git a/src/usuario/usuario.module.ts b/src/usuario/usuario.module.ts index c9ac99d..65dd063 100644 --- a/src/usuario/usuario.module.ts +++ b/src/usuario/usuario.module.ts @@ -18,5 +18,6 @@ import { TipoUsuarioModule } from '../tipo-usuario/tipo-usuario.module'; ], controllers: [UsuarioController], providers: [UsuarioService], + exports: [UsuarioService], }) export class UsuarioModule {} diff --git a/src/usuario/usuario.service.ts b/src/usuario/usuario.service.ts index e5720ae..d2158e7 100644 --- a/src/usuario/usuario.service.ts +++ b/src/usuario/usuario.service.ts @@ -2,7 +2,6 @@ import { ConflictException, Injectable, NotFoundException, - UnauthorizedException, } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; @@ -84,17 +83,6 @@ export class UsuarioService { return password; }; - login(usuario: string, password: string) { - return this.findByUsuario(usuario).then((usuario) => { - if (!this.bcryptService.comparar(password, usuario.password)) - throw new UnauthorizedException( - 'Usuario y/o contraseña incorrectos, trata de nuevo.', - ); - /* Crear JWT y regresarlo */ - return usuario; - }); - } - passwordReset(id_usuario: number) { const password = this.generarPassword();