diff --git a/backend/src/user/exception/UserNotFoundException.ts b/backend/src/user/exception/UserNotFoundException.ts new file mode 100644 index 00000000..d2f9c13f --- /dev/null +++ b/backend/src/user/exception/UserNotFoundException.ts @@ -0,0 +1,11 @@ +import { BaseException } from '@src/common/exception/BaseException'; + +export class UserNotFoundException extends BaseException { + constructor(id: number) { + super({ + code: 2001, + message: `id:${id} 사용자를 찾을 수 없습니다.`, + status: 404, + }); + } +} diff --git a/backend/src/user/exception/UserPermissionException.ts b/backend/src/user/exception/UserPermissionException.ts deleted file mode 100644 index 145df4fd..00000000 --- a/backend/src/user/exception/UserPermissionException.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { BaseException } from '@src/common/exception/BaseException'; - -export class UserPermissionException extends BaseException { - constructor(id: number) { - super({ - code: 2001, - message: `id:${id} 유저에 대한 권한이 없습니다.`, - status: 403, - }); - } -} diff --git a/backend/src/user/guards/UserPermissionGuard.ts b/backend/src/user/guards/UserPermissionGuard.ts deleted file mode 100644 index 5ceb97ba..00000000 --- a/backend/src/user/guards/UserPermissionGuard.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; -import { UserService } from '@src/user/user.service'; -import { UserPermissionException } from '@src/user/exception/UserPermissionException'; - -@Injectable() -export class UserPermissionGuard implements CanActivate { - constructor(private readonly userService: UserService) {} - - async canActivate(context: ExecutionContext): Promise { - const request = context.switchToHttp().getRequest(); - const userId = Number(request.params.id); - const requesterId = Number(request.user.userId); - - const user = await this.userService.getUserInfo(userId); - if (user.id !== requesterId) { - throw new UserPermissionException(userId); - } - return true; - } -} diff --git a/backend/src/user/user.controller.ts b/backend/src/user/user.controller.ts index 2db73c62..ed99a104 100644 --- a/backend/src/user/user.controller.ts +++ b/backend/src/user/user.controller.ts @@ -1,15 +1,15 @@ -import { Controller, Get, Param, UseGuards } from '@nestjs/common'; +import { Controller, Get, UseGuards } from '@nestjs/common'; import { UserService } from './user.service'; import { JwtAuthGuard } from '@src/auth/JwtAuthGuard'; -import { UserPermissionGuard } from '@src/user/guards/UserPermissionGuard'; +import { AuthUser } from '@src/auth/AuthUser.decorator'; @Controller('users') export class UserController { constructor(private readonly userService: UserService) {} - @Get('/:id') - @UseGuards(JwtAuthGuard, UserPermissionGuard) - async getUserInfo(@Param('id') id: number) { - return await this.userService.getUserInfo(id); + @Get('/info') + @UseGuards(JwtAuthGuard) + async getUserInfo(@AuthUser() user: AuthUser) { + return await this.userService.getUserInfo(user.userId); } } diff --git a/backend/src/user/user.service.ts b/backend/src/user/user.service.ts index 14505301..61acc2d1 100644 --- a/backend/src/user/user.service.ts +++ b/backend/src/user/user.service.ts @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common'; import { UserRepository } from './user.repository'; import { CreateUserRequest } from './dto/CreateUserRequest'; import { UserIconResponse } from '@src/user/dto/UserIconResponse'; +import { UserNotFoundException } from '@src/user/exception/UserNotFoundException'; @Injectable() export class UserService { @@ -26,6 +27,9 @@ export class UserService { async getUserInfo(userId: number) { const user = await this.userRepository.findById(userId); + if (!user) { + throw new UserNotFoundException(userId); + } return UserIconResponse.from(user); } }