From 904db0549fcead9d9062792ff2c2d5f82a247c40 Mon Sep 17 00:00:00 2001 From: evenegas Date: Fri, 27 Jun 2025 21:49:50 -0600 Subject: [PATCH] =?UTF-8?q?ultima=20versi=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.module.ts | 20 +++++++++++++------- src/auth/auth.module.ts | 15 ++++++--------- src/auth/auth.service.ts | 23 ++++++++++++++++++----- src/sistema/sistema.service.ts | 22 ++++++++++++---------- 4 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/app.module.ts b/src/app.module.ts index 31f1b7a..413c3ad 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -2,10 +2,11 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { MailModule } from './mail/mail.module'; -import {ExcelModule} from './excel/excel.module' +import { ExcelModule } from './excel/excel.module' import { TypeOrmModule } from '@nestjs/typeorm'; import { ConfigModule } from '@nestjs/config'; import { SistemaModule } from './sistema/sistema.module'; +import { JwtModule } from '@nestjs/jwt'; @@ -13,11 +14,16 @@ import { SistemaModule } from './sistema/sistema.module'; @Module({ imports: [ ConfigModule.forRoot( - {isGlobal: true} + { isGlobal: true } ), - + + JwtModule.register({ + secret: process.env.JWT_SECRET, + + }), + TypeOrmModule.forRoot({ - type: 'mysql', + type: 'mysql', host: process.env.db_host, username: process.env.db_username, database: process.env.db_database, @@ -27,9 +33,9 @@ import { SistemaModule } from './sistema/sistema.module'; dropSchema: false, // elimina la base de datos // logging: true, // Habilita los logs para depuración autoLoadEntities: true, // Carga automáticamente las entidades - + }), - + MailModule, ExcelModule, SistemaModule, @@ -39,4 +45,4 @@ import { SistemaModule } from './sistema/sistema.module'; controllers: [AppController], providers: [AppService], }) -export class AppModule {} +export class AppModule { } diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index c7c081d..dff7694 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -7,16 +7,13 @@ import { AuthService } from './auth.service'; @Module({ imports: [ ConfigModule, // Ya es global, pero lo puedes dejar - JwtModule.registerAsync({ - imports: [ConfigModule], - inject: [ConfigService], - useFactory: (config: ConfigService) => ({ - secret: config.get('JWT_SECRET'), - signOptions: { expiresIn: '1d' }, - }), - }), + JwtModule.register({ + secret: process.env.JWT_SECRET + }) + + ], providers: [AuthService], exports: [AuthService, JwtModule], }) -export class AuthModule {} +export class AuthModule { } diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 68117d3..bcb516c 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -6,20 +6,33 @@ export class AuthService { constructor(private jwtService: JwtService) { } generarToken(payload: any) { + console.log('JWT_SECRET:', process.env.JWT_SECRET); return this.jwtService.sign(payload, { secret: process.env.JWT_SECRET, - expiresIn: '1d', + }); } - verificarToken(token: string) { + async verificarToken(token: string) { + console.log('JWT_SECRET:', process.env.JWT_SECRET); + console.log('JWT_SECRET:', process.env.JWT_SECRET); + console.log('Verificando token:', token); + try { - return this.jwtService.verify(token, { + const result = await this.jwtService.verify(token, { secret: process.env.JWT_SECRET, }); - } catch { - return null; + console.log('Resultado de verificación:', result); + return result; + + } catch (error) { + + + throw new Error('Token inválido o expirado'); } + + + } } diff --git a/src/sistema/sistema.service.ts b/src/sistema/sistema.service.ts index 603ff23..2efa8d1 100644 --- a/src/sistema/sistema.service.ts +++ b/src/sistema/sistema.service.ts @@ -11,7 +11,7 @@ export class SistemaService { constructor( @InjectRepository(Sistema) private readonly sistemaRepository: Repository, private readonly authService: AuthService - ) {} + ) { } async Generar_Token(nombreSistema: string, adminPassword: string) { if (adminPassword !== process.env.ADMIN_PASSWORD) { @@ -23,16 +23,18 @@ export class SistemaService { throw new NotFoundException('Sistema no encontrado'); } - const token = this.authService.generarToken({ sistemaId: sistema.id_sistema, nombre: sistema.nombre_sistema, ip:sistema.ip }); + const token = this.authService.generarToken({ sistemaId: sistema.id_sistema, nombre: sistema.nombre_sistema, ip: sistema.ip }); return token; } async acceso(token: string) { - const decoded = this.authService.verificarToken(token); + const decoded = await this.authService.verificarToken(token); if (!decoded) { throw new UnauthorizedException('Token inválido'); } + + const sistema = await this.sistemaRepository.findOneBy({ id_sistema: decoded.sistemaId }); if (!sistema) { throw new UnauthorizedException('Sistema no encontrado'); @@ -46,18 +48,18 @@ export class SistemaService { throw new UnauthorizedException('Password erróneo'); } - const {encryptedData:encryptedData_token,iv:encryptedData_iv}= encrypt(createSistemaDto.refreshToken) + const { encryptedData: encryptedData_token, iv: encryptedData_iv } = encrypt(createSistemaDto.refreshToken) - const { encryptedData:encrypted_password, iv:iv_password } = encrypt(createSistemaDto.email_password); + const { encryptedData: encrypted_password, iv: iv_password } = encrypt(createSistemaDto.email_password); const sistema = this.sistemaRepository.create({ nombre_sistema: createSistemaDto.Nombre, ip: createSistemaDto.ip, - email:createSistemaDto.email, + email: createSistemaDto.email, email_password_encrypted: encrypted_password, email_password_iv: iv_password, clientId: createSistemaDto.clientId, - refreshToken_encrypted:encryptedData_token, + refreshToken_encrypted: encryptedData_token, refreshToken_iv: encryptedData_iv @@ -69,9 +71,9 @@ export class SistemaService { return this.Generar_Token(createSistemaDto.Nombre, createSistemaDto.password); } - async findByNombre(sistema:string){ - const sistem= this.sistemaRepository.findOne({where:{nombre_sistema:sistema}}) - if(!sistem){ + async findByNombre(sistema: string) { + const sistem = this.sistemaRepository.findOne({ where: { nombre_sistema: sistema } }) + if (!sistem) { throw new UnauthorizedException('Password erróneo'); }