首页 > 解决方案 > 带有 NestJS 的 Swagger 不会为某些端点附加 Bearer 令牌

问题描述

我有一个 NestJS 控制器:


@Controller('users')
@ApiTags('users')
export class UserController {
    constructor(
        private userService: UserService,
        private userRoleService: UserRoleService,
        private readonly translationService: TranslationService,
    ) {}
    @Get('organization-admin')
    @AuthorizeFor(RoleType.ORGANIZATION_ADMIN)
    @HttpCode(HttpStatus.OK)
    @ApiOkResponse({
        description: 'Access for organization admin granted'
    })
    async organizationAdmin(@AuthUser() user: UserEntity): Promise<string> {
        return `Hello organization admin ${user.firstName}`;
    }

    @Post(':id/assign-role')
    @AuthorizeFor(RoleType.ORGANIZATION_ADMIN)
    @HttpCode(HttpStatus.OK)
    @ApiOkResponse({
        status: HttpStatus.OK,
        description: 'Role assigned to user',
        type: UserRoleDto,
    })
    async assignRoleToUser(
        @Body() userRole: CreateUserRoleDto,
        @UUIDParam('id') userId: string
    ): Promise<CreateUserRoleDto> {
        const user = await this.userService.findOne({ id: userId });
        return this.userRoleService.createUserRole(user, userRole);
    }
}

使用 swagger - Bearer 令牌附加到请求@Get('organization-admin')但不会附加到@Post(':id/assign-role').

你能帮我弄清楚为什么会发生吗?

提前致谢。

标签: swaggernestjsnestjs-swagger

解决方案


尝试将@ApiBearerAuth()装饰器添加到您的端点/控制器


推荐阅读