Closed abuzain432432 closed 2 months ago
Ah, I see! I figured it out, and it seems that the issue is related to how I am extracting the metadata from the context. According to the documentation, the actual Roles decorator is passed as an argument to get the metadata, but I was using a string. Correct way:
const roles = this.reflector.getAllAndOverride<undefined | RolesEnum[]>(
Roles,
[context.getHandler(), context.getClass()],
);
Incorrect way:
const roles = this.reflector.getAllAndOverride<undefined | RolesEnum[]>(
'roles',
[context.getHandler(), context.getClass()],
);
Is there an existing issue for this?
Current behavior
I am using the Roles Decorator in my controller, but I don't know why this is not working. I have added a log in my code, and I am getting
undefined
. muuve_nestjs_api-1 | ++++++++++++++++++++++++ muuve_nestjs_api-1 | undefined muuve_nestjs_api-1 | +++++++++++++++++++++Controller
Guard:
Minimum reproduction code
https://docs.nestjs.com/guards#setting-roles-per-handler
Steps to reproduce
Copy and paste the code into any new NestJS project and send a request to a route where the Roles decorator is used.
Expected behavior
When I use the explicit implementation with SetMetadata like this, it is working fine π, and my routes are being protected accurately.
I am very interested in knowing what is wrong with the implementation. Is there something wrong with the way I used the Roles decorator, or is there some other problem?
Package
Other package
No response
NestJS version
No response
Packages versions
Node.js version
20
In which operating systems have you tested?
Other
No response