bc5ddb29c7
- Introduced TipoEvento entity with enum for event types. - Created DTOs for creating and updating TipoEvento. - Implemented TipoEvento service with methods for CRUD operations and seeding initial data. - Added TipoEvento controller for handling HTTP requests related to event types. - Integrated TipoEvento into the main application module. - Updated Evento entity to include a relationship with TipoEvento. - Enhanced ParticipanteEvento service to register attendance using QR tokens. - Implemented QR token generation and validation for event attendance. - Updated API documentation for new endpoints and functionalities. - Adjusted TypeOrm configuration to include new entities.
53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import {
|
|
CanActivate,
|
|
ExecutionContext,
|
|
Injectable,
|
|
UnauthorizedException,
|
|
} from '@nestjs/common';
|
|
import { JwtService } from '@nestjs/jwt';
|
|
import { AdministradorService } from 'src/administrador/administrador.service';
|
|
|
|
@Injectable()
|
|
export class JwtValidationGuard implements CanActivate {
|
|
constructor(
|
|
private jwtService: JwtService,
|
|
private administradorService: AdministradorService,
|
|
) {}
|
|
|
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
const request = context.switchToHttp().getRequest();
|
|
const authHeader = request.headers.authorization;
|
|
|
|
if (!authHeader) {
|
|
throw new UnauthorizedException('Token no proporcionado');
|
|
}
|
|
|
|
const token = authHeader.split(' ')[1];
|
|
|
|
try {
|
|
const decodedToken = this.jwtService.verify(token, {
|
|
secret: process.env.JWT_SECRET,
|
|
});
|
|
|
|
const administrador = await this.administradorService.getAdminById(
|
|
decodedToken.sub,
|
|
);
|
|
|
|
if (!administrador) {
|
|
throw new UnauthorizedException('Usuario no encontrado');
|
|
}
|
|
|
|
request.user = administrador;
|
|
} catch (error) {
|
|
if (error.name === 'TokenExpiredError') {
|
|
throw new UnauthorizedException(
|
|
`La sesión ha expirado, inicia sesión nuevamente.`,
|
|
);
|
|
}
|
|
throw new UnauthorizedException('Token inválido');
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|