Skip to content

Commit

Permalink
feat: 내 코스 리스트 조회 api 에서 유저 인증정보 추가 #83
Browse files Browse the repository at this point in the history
  • Loading branch information
koomchang committed Nov 9, 2024
1 parent d16a680 commit a75ebfc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion backend/src/auth/AuthUser.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}
6 changes: 2 additions & 4 deletions backend/src/auth/JwtAuthGuard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions backend/src/course/course.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down

0 comments on commit a75ebfc

Please sign in to comment.