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'
|
import { Role } from '../guards/role.enum'
|
||||||
|
|
||||||
export const ROLES_KEY = 'roles'
|
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 { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'
|
||||||
import { Reflector } from '@nestjs/core'
|
import { Reflector } from '@nestjs/core'
|
||||||
import { Observable } from 'rxjs'
|
import { Observable } from 'rxjs'
|
||||||
import { ROLES_KEY } from '../decorators/has-roles.decorator'
|
import { ROLES_KEY } from '../decorators/roles.decorator'
|
||||||
import { Role } from './role.enum'
|
import { Role } from './role.enum'
|
||||||
|
|
||||||
//참고: https://shpota.com/2022/07/16/role-based-authorization-with-jwt-using-nestjs.html
|
//참고: 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 { User } from '@prisma/client'
|
||||||
import { UserDTO } from './dtos/user.dto'
|
import { UserDTO } from './dtos/user.dto'
|
||||||
import { UserService } from './user.service'
|
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 { JwtAuthGuard } from '../auth/guards/jwt.authguard'
|
||||||
import { Role } from '../auth/guards/role.enum'
|
import { Role } from '../auth/guards/role.enum'
|
||||||
import { RolesGuard } from '../auth/guards/roles.guard'
|
import { RolesGuard } from '../auth/guards/roles.guard'
|
||||||
@ -127,14 +127,14 @@ export class UserController {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@HasRoles(Role.USER)
|
@Roles(Role.USER)
|
||||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||||
@Get(':id')
|
@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) })
|
return await this.userService.fetchOne({ id: Number(id) })
|
||||||
}
|
}
|
||||||
|
|
||||||
// @HasRoles(Role.USER)
|
// @Roles(Role.USER)
|
||||||
// @UseGuards(JwtAuthGuard, RolesGuard)
|
// @UseGuards(JwtAuthGuard, RolesGuard)
|
||||||
//@UseGuards(JwtAuthGuard)
|
//@UseGuards(JwtAuthGuard)
|
||||||
@Post()
|
@Post()
|
||||||
@ -142,7 +142,7 @@ export class UserController {
|
|||||||
return await this.userService.add(data)
|
return await this.userService.add(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@HasRoles(Role.USER)
|
@Roles(Role.USER)
|
||||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||||
@Put(':id')
|
@Put(':id')
|
||||||
async update(@Param('id') id: string, @Body() data: UserDTO): Promise<User> {
|
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)
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||||
@Delete(':id')
|
@Delete(':id')
|
||||||
async delete(@Param('id') id: string): Promise<User | undefined> {
|
async delete(@Param('id') id: string): Promise<User | undefined> {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user