diff --git a/backend/src/auth/AuthUser.decorator.ts b/backend/src/auth/AuthUser.decorator.ts index d3582dbe..58f89910 100644 --- a/backend/src/auth/AuthUser.decorator.ts +++ b/backend/src/auth/AuthUser.decorator.ts @@ -25,7 +25,7 @@ function isAuthUser(obj: any): obj is AuthUser { return ( typeof obj === 'object' && obj !== null && - typeof obj.userId === 'number' && + !isNaN(obj.userId) && typeof obj.role === 'string' ); } diff --git a/backend/src/auth/JwtAuthGuard.ts b/backend/src/auth/JwtAuthGuard.ts index 07027a1e..6c86e4fe 100644 --- a/backend/src/auth/JwtAuthGuard.ts +++ b/backend/src/auth/JwtAuthGuard.ts @@ -4,6 +4,7 @@ import { TokenExpiredError } from 'jsonwebtoken'; import { ConfigService } from '@nestjs/config'; import { AuthenticationException } from './exception/AuthenticationException'; import { extractBearerToken } from './utils'; +import { AuthUser } from './AuthUser.decorator'; @Injectable() export class JwtAuthGuard implements CanActivate { @@ -20,10 +21,7 @@ export class JwtAuthGuard implements CanActivate { throw new AuthenticationException('토큰이 없습니다.'); } try { - request.user = jwt.verify(token, this.jwtSecretKey) as { - userId: string; - role: string; - }; + request.user = jwt.verify(token, this.jwtSecretKey) as AuthUser; return true; } catch (error) { if (error instanceof TokenExpiredError) { diff --git a/backend/src/course/course.controller.ts b/backend/src/course/course.controller.ts index 7dc5a3d8..171595e2 100644 --- a/backend/src/course/course.controller.ts +++ b/backend/src/course/course.controller.ts @@ -8,11 +8,14 @@ import { Param, Patch, Put, + UseGuards, } from '@nestjs/common'; import { CreateCourseRequest } from './dto/CreateCourseRequest'; import { UpdateCourseInfoRequest } from './dto/UpdateCourseInfoRequest'; import { CourseService } from './course.service'; import { SetPlacesOfCourseRequest } from './dto/AddPlaceToCourseRequest'; +import { JwtAuthGuard } from '../auth/JwtAuthGuard'; +import { AuthUser } from '../auth/AuthUser.decorator'; @Controller('/courses') export class CourseController { @@ -31,8 +34,9 @@ export class CourseController { } @Get('/my') - async getMyCourseList() { - const userId = 1; // Todo. 로그인 기능 완성 후 수정 + @UseGuards(JwtAuthGuard) + async getMyCourseList(@AuthUser() user: AuthUser) { + const userId = Number(user.userId); return await this.courseService.getOwnCourses(userId); }