Skip to content

Commit

Permalink
Merge pull request #32 from mash-up-kr/feature/public-decorator
Browse files Browse the repository at this point in the history
Public 데코레이터 추가 및 GetUser데코레이터 Failover처리 수정
  • Loading branch information
J-Hoplin authored Jul 20, 2024
2 parents dfee598 + 747719d commit fc1269c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/common/decorators/getUser.decorator.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -18,6 +22,9 @@ export const GetUser = createParamDecorator(
const request = context.switchToHttp().getRequest<Request>();
// 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'];
},
);
1 change: 1 addition & 0 deletions src/common/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './getUser.decorator';
export * from './public.decorator';
11 changes: 11 additions & 0 deletions src/common/decorators/public.decorator.ts
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);
24 changes: 23 additions & 1 deletion src/modules/users/guards/jwt.guard.ts
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;
}
}

0 comments on commit fc1269c

Please sign in to comment.