首页 > 解决方案 > How to write logic inside Nest js Middleware and Guard

问题描述

I want to make Multiple role Access Project Where I have to allow user according to their roles , so for this I have to create multiple Middleware & Guard according to Roles but there is no documentation how to get current login user details inside Nest Middleware and also how to get current login user details from jsonWebToken inside Nest Guard & Middleware,

I have Users collection schema is Like that

UsersCollectionSchema: {
  name: '',
  email:'',
  password:'',
  roles: [ admin, editor]
}

标签: node.jsnestjs

解决方案


用户通过身份验证后,我建议您将用户对象附加到请求中,这是常见的方式,以便通过请求访问中间件内部的用户,并通过可以从中获取的请求访问守卫内部的用户执行上下文

这是一个示例https://github.com/nestjs/nest/blob/master/sample/01-cats-app/src/common/guards/roles.guard.ts


推荐阅读