ultima versión
This commit is contained in:
+13
-7
@@ -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 { }
|
||||
|
||||
@@ -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 { }
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ export class SistemaService {
|
||||
constructor(
|
||||
@InjectRepository(Sistema) private readonly sistemaRepository: Repository<Sistema>,
|
||||
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');
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user