From d633bb0601e4c60186e7b46375b0cc3a99be9fa1 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:58:19 +0900 Subject: [PATCH] nestjs_auth ini... --- .../{has-roles.decorator.ts => roles.decorator.ts} | 2 +- src/auth/guards/roles.guard.ts | 2 +- src/user/user.controller.ts | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) rename src/auth/decorators/{has-roles.decorator.ts => roles.decorator.ts} (61%) diff --git a/src/auth/decorators/has-roles.decorator.ts b/src/auth/decorators/roles.decorator.ts similarity index 61% rename from src/auth/decorators/has-roles.decorator.ts rename to src/auth/decorators/roles.decorator.ts index 3fedb2c..63374d9 100644 --- a/src/auth/decorators/has-roles.decorator.ts +++ b/src/auth/decorators/roles.decorator.ts @@ -2,4 +2,4 @@ import { SetMetadata } from '@nestjs/common' import { Role } from '../guards/role.enum' export const ROLES_KEY = 'roles' -export const HasRoles = (...roles: Role[]) => SetMetadata(ROLES_KEY, roles) +export const Roles = (...roles: Role[]) => SetMetadata(ROLES_KEY, roles) diff --git a/src/auth/guards/roles.guard.ts b/src/auth/guards/roles.guard.ts index ceee602..2119c12 100644 --- a/src/auth/guards/roles.guard.ts +++ b/src/auth/guards/roles.guard.ts @@ -1,7 +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 { ROLES_KEY } from '../decorators/roles.decorator' import { Role } from './role.enum' //참고: https://shpota.com/2022/07/16/role-based-authorization-with-jwt-using-nestjs.html diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index ab0f78c..5dd32c1 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -13,7 +13,7 @@ import { import { User } from '@prisma/client' import { UserDTO } from './dtos/user.dto' import { UserService } from './user.service' -import { HasRoles } from '../auth/decorators/has-roles.decorator' +import { Roles } from '../auth/decorators/roles.decorator' import { JwtAuthGuard } from '../auth/guards/jwt.authguard' import { Role } from '../auth/guards/role.enum' import { RolesGuard } from '../auth/guards/roles.guard' @@ -127,14 +127,14 @@ export class UserController { return result } - @HasRoles(Role.USER) + @Roles(Role.USER) @UseGuards(JwtAuthGuard, RolesGuard) @Get(':id') - async fetchOne(@Param('id') id: number): Promise { + async fetchOne(@Param('id') id: string): Promise { return await this.userService.fetchOne({ id: Number(id) }) } - // @HasRoles(Role.USER) + // @Roles(Role.USER) // @UseGuards(JwtAuthGuard, RolesGuard) //@UseGuards(JwtAuthGuard) @Post() @@ -142,7 +142,7 @@ export class UserController { return await this.userService.add(data) } - @HasRoles(Role.USER) + @Roles(Role.USER) @UseGuards(JwtAuthGuard, RolesGuard) @Put(':id') async update(@Param('id') id: string, @Body() data: UserDTO): Promise { @@ -153,7 +153,7 @@ export class UserController { }) } - @HasRoles(Role.USER) + @Roles(Role.USER) @UseGuards(JwtAuthGuard, RolesGuard) @Delete(':id') async delete(@Param('id') id: string): Promise {