From 922701306a6bbf472ca0270c50e30deee972ee1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Fri, 2 Sep 2022 12:44:30 +0900 Subject: [PATCH] nestjs_auth ini... --- src/auth/decorators/has-roles.decorator.ts | 3 ++- src/auth/guards/roles.guard.ts | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/auth/decorators/has-roles.decorator.ts b/src/auth/decorators/has-roles.decorator.ts index ed5ce67..3fedb2c 100644 --- a/src/auth/decorators/has-roles.decorator.ts +++ b/src/auth/decorators/has-roles.decorator.ts @@ -1,4 +1,5 @@ import { SetMetadata } from '@nestjs/common' import { Role } from '../guards/role.enum' -export const HasRoles = (...roles: Role[]) => SetMetadata('has-roles', roles) +export const ROLES_KEY = 'roles' +export const HasRoles = (...roles: Role[]) => SetMetadata(ROLES_KEY, roles) diff --git a/src/auth/guards/roles.guard.ts b/src/auth/guards/roles.guard.ts index 7e39da4..ceee602 100644 --- a/src/auth/guards/roles.guard.ts +++ b/src/auth/guards/roles.guard.ts @@ -1,6 +1,7 @@ import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common' import { Reflector } from '@nestjs/core' import { Observable } from 'rxjs' +import { ROLES_KEY } from '../decorators/has-roles.decorator' import { Role } from './role.enum' //참고: https://shpota.com/2022/07/16/role-based-authorization-with-jwt-using-nestjs.html @@ -11,10 +12,10 @@ export class RolesGuard implements CanActivate { canActivate( context: ExecutionContext ): boolean | Promise | Observable { - const requiredRoles = this.reflector.getAllAndOverride( - 'has-roles', - [context.getHandler(), context.getClass()] - ) + const requiredRoles = this.reflector.getAllAndOverride(ROLES_KEY, [ + context.getHandler(), + context.getClass() + ]) if (!requiredRoles) { return true }