Agregar decorador @Public() para endpoints públicos
- Crear public.decorator.ts con IS_PUBLIC_KEY - Modificar JwtAuthGuard para verificar @Public() y saltar auth - Marcar GET /tourism/guides como público Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
4
src/common/decorators/public.decorator.ts
Normal file
4
src/common/decorators/public.decorator.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
|
||||
export const IS_PUBLIC_KEY = 'isPublic';
|
||||
export const Public = () => SetMetadata(IS_PUBLIC_KEY, true);
|
||||
@@ -1,5 +1,24 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, ExecutionContext } from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { IS_PUBLIC_KEY } from '../decorators/public.decorator';
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {}
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||
constructor(private reflector: Reflector) {
|
||||
super();
|
||||
}
|
||||
|
||||
canActivate(context: ExecutionContext) {
|
||||
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
]);
|
||||
|
||||
if (isPublic) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.canActivate(context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import { UpdatePlaceDto } from './dto/update-place.dto';
|
||||
import { CreateTourGuideDto } from './dto/create-tour-guide.dto';
|
||||
import { UpdateTourGuideDto } from './dto/update-tour-guide.dto';
|
||||
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
||||
import { Public } from '../../common/decorators/public.decorator';
|
||||
import { RolesGuard } from '../../common/guards/roles.guard';
|
||||
import { Roles } from '../../common/decorators/roles.decorator';
|
||||
import { Destination } from '../../entities/destination.entity';
|
||||
@@ -154,6 +155,7 @@ export class TourismController {
|
||||
return this.tourismService.createTourGuide(createTourGuideDto);
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Get('guides')
|
||||
@ApiOperation({ summary: 'Get all tour guides with filters' })
|
||||
@ApiQuery({ name: 'page', required: false, type: Number })
|
||||
|
||||
Reference in New Issue
Block a user