diff --git a/backend/src/auth/AuthUser.decorator.ts b/backend/src/auth/AuthUser.decorator.ts new file mode 100644 index 00000000..d3582dbe --- /dev/null +++ b/backend/src/auth/AuthUser.decorator.ts @@ -0,0 +1,31 @@ +import { createParamDecorator, ExecutionContext } from '@nestjs/common'; +import { AuthenticationException } from './exception/AuthenticationException'; + +export const AuthUser = createParamDecorator( + (data: unknown, ctx: ExecutionContext) => { + const requestingUser = ctx.switchToHttp().getRequest().user; + if (!requestingUser) { + throw new AuthenticationException('인증 정보가 없습니다.'); + } + + if (!isAuthUser(requestingUser)) { + throw new AuthenticationException('유효하지 않은 인증 정보입니다.'); + } + + return requestingUser as AuthUser; + }, +); + +export interface AuthUser { + userId: string; + role: string; +} + +function isAuthUser(obj: any): obj is AuthUser { + return ( + typeof obj === 'object' && + obj !== null && + typeof obj.userId === 'number' && + typeof obj.role === 'string' + ); +} diff --git a/backend/src/place/place.controller.ts b/backend/src/place/place.controller.ts index f5a49499..bf4888bb 100644 --- a/backend/src/place/place.controller.ts +++ b/backend/src/place/place.controller.ts @@ -1,11 +1,21 @@ -import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common'; +import { + Body, + Controller, + Get, + Param, + Post, + Query, + UseGuards, +} from '@nestjs/common'; import { PlaceService } from './place.service'; import { CreatePlaceRequest } from './dto/CreatePlaceRequest'; +import { JwtAuthGuard } from '../auth/JwtAuthGuard.'; @Controller('places') export class PlaceController { constructor(private readonly placeService: PlaceService) {} + @UseGuards(JwtAuthGuard) @Post() async addPlace(@Body() createPlaceDto: CreatePlaceRequest) { return this.placeService.addPlace(createPlaceDto);