首页 > 解决方案 > 用户验证在哪里完成?

问题描述

抱歉,我是 nestJs 的新手,我正在使用来自 github 的示例应用程序:

https://github.com/lujakob/nestjs-realworld-example-app

我的问题是关于如何正式对有权修改特定文章的用户进行验证。通常,这部分代码应该有一个验证:

@ApiOperation({ summary: 'Update article' })
@ApiResponse({ status: 201, description: 'The article has been successfully updated.'})
@ApiResponse({ status: 403, description: 'Forbidden.' })
@put(':slug')
async update(@param() params, @Body('article') articleData: CreateArticleDto) {
// Todo: update slug also when title gets changed
return this.articleService.update(params.slug, articleData);
}

所以不是每个人都可以修改别人的文章。

谢谢你,

标签: javascripttypescriptexpressnestjs

解决方案


NestJS 有 Guards 的概念 https://docs.nestjs.com/guards

守卫是另一种类型的中间件,它将在您的路由处理程序被调用之前执行。Guard 只是一种帮助您保护路线的机制。底层实现(如角色或权限)需要由您实现。


推荐阅读