Can i get path parameters in nestjs guard function using some other way than just looking for a raw request object from http context?
what i want to do for example is
@Patch(':id/someActionName')
@UseGuards(SomeGuard)
async activateRole(@Param('id') id,@Body() input: SomeObject): Promise<any> {
//some logic
return response;
}
and my SomeGuard would get value of 'id' parameter and 'input' parameter, input parameter is easy, but i don't see easy way to get 'id'
In your guard you can access the route parameters by getting the request from the context like so:
canActivate(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
const params = request.params;
const id = params.id; // automatically parsed
}
This wasn't in the documentation and I had the exact same problem as you and had to dig through the request object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With