nestjs_auth ini...
This commit is contained in:
parent
922701306a
commit
d633bb0601
@ -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)
|
||||
@ -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
|
||||
|
||||
@ -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> {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user