javascript - 用户验证在哪里完成?
问题描述
抱歉,我是 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);
}
所以不是每个人都可以修改别人的文章。
谢谢你,
解决方案
NestJS 有 Guards 的概念 https://docs.nestjs.com/guards
守卫是另一种类型的中间件,它将在您的路由处理程序被调用之前执行。Guard 只是一种帮助您保护路线的机制。底层实现(如角色或权限)需要由您实现。
推荐阅读
- jquery - 错误 TS2688 中的错误:找不到“jquery”的类型定义文件。[Angular App] --
- css - IE11 中的 CSS 网格自动行
- php - Laravel 6 中用户定义的 PDF 模板
- excel - 根据可用性和日期 Excel 公式确定 4 个值的优先级
- r - Poisson/Nbinom 分布中的分位数 0.95 或 0.98 给出分布外的值 (R)
- java - 如何从 Kotlin 中的 Java 枚举中获取名称
- java - 凯撒密码Java解密
- flutter - 有没有办法通过flutter查看来自Cloud Firestore的用户的关注者或关注者?
- javascript - 按钮事件侦听器无响应
- qt - Qt Graphics:QImage 的绘画非常像素化