【问题标题】:Where the user verification is done?用户验证在哪里完成?
【发布时间】:2020-09-29 01:00:51
【问题描述】:

对不起,我是 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);
}

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

谢谢,

【问题讨论】:

    标签: javascript typescript express nestjs


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,我知道可以用守卫来实现,但我不明白在我分享的示例中它是如何实现的......我没有看到任何守卫
    • github.com/lujakob/nestjs-realworld-example-app/blob/master/src/… 在您链接的示例中,作者没有使用 Guards,但他使用原始中间件并将中间件附加到 UserModule 中的 user 路由上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    相关资源
    最近更新 更多