-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from mash-up-kr/feature/public-decorator
Public 데코레이터 추가 및 GetUser데코레이터 Failover처리 수정
- Loading branch information
Showing
4 changed files
with
43 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './getUser.decorator'; | ||
export * from './public.decorator'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
|
||
/** | ||
* @Public Decorator | ||
* | ||
* Controller레벨 혹은 애플리케이션 Global Guard가 걸려있는 경우 | ||
* 특정 Route에 대해 Public을 허용할 수 있게 하기 위해 사용합니다. | ||
*/ | ||
|
||
export const PublicRouteToken = Symbol('public-route'); | ||
export const Public = () => SetMetadata(PublicRouteToken, true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<boolean> { | ||
// 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; | ||
} | ||
} |