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 }