From 9717ed1f5ae6e23fb94065a5eb0e904a735ab64f Mon Sep 17 00:00:00 2001 From: J-Hoplin Date: Tue, 2 Jul 2024 00:26:20 +0900 Subject: [PATCH 1/4] =?UTF-8?q?Feature:=20Public=EB=8D=B0=EC=BD=94?= =?UTF-8?q?=EB=A0=88=EC=9D=B4=ED=84=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/decorators/public.decorator.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/common/decorators/public.decorator.ts diff --git a/src/common/decorators/public.decorator.ts b/src/common/decorators/public.decorator.ts new file mode 100644 index 0000000..2eb1572 --- /dev/null +++ b/src/common/decorators/public.decorator.ts @@ -0,0 +1,11 @@ +import { SetMetadata } from '@nestjs/common'; + +/** + * @Public Decorator + * + * Controller레벨 혹은 애플리케이션 Global Guard가 걸려있는 경우 + * 특정 Route에 대해 Public을 허용할 수 있게 하기 위해 사용합니다. + */ + +export const PublicRouteToken = 'public-route'; +export const Public = () => SetMetadata(PublicRouteToken, true); From bc0284553b31465de2cdf38f10f861c5ebc3acd1 Mon Sep 17 00:00:00 2001 From: J-Hoplin Date: Tue, 2 Jul 2024 00:56:52 +0900 Subject: [PATCH 2/4] =?UTF-8?q?Feat:=20Guard=EC=97=90=EC=84=9C=20@Public?= =?UTF-8?q?=20=EB=A9=94=ED=83=80=EB=8D=B0=EC=9D=B4=ED=84=B0=EC=97=90=20?= =?UTF-8?q?=EB=8C=80=ED=95=9C=20=EB=B6=84=EA=B8=B0=EB=A5=BC=20=ED=95=A0=20?= =?UTF-8?q?=EC=88=98=20=EC=9E=88=EB=8F=84=EB=A1=9D=20canActivate=20Overrid?= =?UTF-8?q?e=EC=A7=84=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/decorators/index.ts | 1 + src/modules/users/guards/jwt.guard.ts | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/common/decorators/index.ts b/src/common/decorators/index.ts index 9529afe..e21a29f 100644 --- a/src/common/decorators/index.ts +++ b/src/common/decorators/index.ts @@ -1 +1,2 @@ export * from './getUser.decorator'; +export * from './public.decorator'; diff --git a/src/modules/users/guards/jwt.guard.ts b/src/modules/users/guards/jwt.guard.ts index 0e5ef76..147d90f 100644 --- a/src/modules/users/guards/jwt.guard.ts +++ b/src/modules/users/guards/jwt.guard.ts @@ -1,11 +1,33 @@ import { AuthGuard } from '@nestjs/passport'; -import { Injectable } from '@nestjs/common'; +import { ExecutionContext, Injectable } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; import { JWT_STRATEGY_TOKEN } from '@src/modules/users/guards/strategy/strategy.token'; +import { PublicRouteToken } from '@src/common'; @Injectable() export class JwtGuard extends AuthGuard(JWT_STRATEGY_TOKEN) { constructor(private readonly reflector: Reflector) { super(); } + + async canActivate(context: ExecutionContext): Promise { + // Handler 혹은 Class중 하나만 정의되어있다면 boolean으로 둘다 정의되어있다면 array([boolean,boolean])로 옵니다 + const publicMetadata = this.reflector.getAllAndMerge(PublicRouteToken, [ + context.getClass(), + context.getHandler(), + ]); + + // Handler와 Class모두 메타데이터가 정의되어있는 경우 + if ( + (Array.isArray(publicMetadata) && publicMetadata.length) || + typeof publicMetadata === 'boolean' + ) { + try { + return (await super.canActivate(context)) as boolean; + } catch (err) { + return true; + } + } + return (await super.canActivate(context)) as boolean; + } } From 5190e269050f68dd438c81c93b274cc87867ef5c Mon Sep 17 00:00:00 2001 From: J-Hoplin Date: Tue, 2 Jul 2024 00:58:06 +0900 Subject: [PATCH 3/4] =?UTF-8?q?Fix:=20@GetUser=EB=8D=B0=EC=BD=94=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=EC=97=90=EC=84=9C=20Request=20User=EA=B0=80?= =?UTF-8?q?=20undefined=EC=9D=B8=20=EA=B2=BD=EC=9A=B0=20failover=ED=95=A0?= =?UTF-8?q?=20=EC=88=98=20=EC=9E=88=EB=8F=84=EB=A1=9D=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=20=EB=B0=98=ED=99=98:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/decorators/getUser.decorator.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/common/decorators/getUser.decorator.ts b/src/common/decorators/getUser.decorator.ts index fbf7f53..3a0aa11 100644 --- a/src/common/decorators/getUser.decorator.ts +++ b/src/common/decorators/getUser.decorator.ts @@ -1,4 +1,8 @@ -import { ExecutionContext, createParamDecorator } from '@nestjs/common'; +import { + ExecutionContext, + UnauthorizedException, + createParamDecorator, +} from '@nestjs/common'; import { ReqUserPayload } from '../types/type'; import { Request } from 'express'; @@ -19,6 +23,9 @@ export const GetUser = createParamDecorator( // Expect Request, user property as type 'ReqUserPayload'(Refer to defined in common/types/type.d.ts) const user: Express.User = request.user; + if (!user) { + throw new UnauthorizedException('인증에 실패하였습니다.'); + } return user['id']; }, ); From 747719ddb7f142731358410a09c60de900f41c98 Mon Sep 17 00:00:00 2001 From: J-hoplin1 Date: Sat, 20 Jul 2024 15:28:29 +0900 Subject: [PATCH 4/4] Fix: Fix Public Route Token as string to Symbol --- src/common/decorators/public.decorator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/decorators/public.decorator.ts b/src/common/decorators/public.decorator.ts index 2eb1572..4158662 100644 --- a/src/common/decorators/public.decorator.ts +++ b/src/common/decorators/public.decorator.ts @@ -7,5 +7,5 @@ import { SetMetadata } from '@nestjs/common'; * 특정 Route에 대해 Public을 허용할 수 있게 하기 위해 사용합니다. */ -export const PublicRouteToken = 'public-route'; +export const PublicRouteToken = Symbol('public-route'); export const Public = () => SetMetadata(PublicRouteToken, true);