nestjs_auth ini...

This commit is contained in:
최준흠 2022-09-02 12:58:19 +09:00
parent 922701306a
commit d633bb0601
3 changed files with 8 additions and 8 deletions

View File

@ -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)

View File

@ -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

View File

@ -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<User | undefined> {
async fetchOne(@Param('id') id: string): Promise<User | undefined> {
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<User> {
@ -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<User | undefined> {