I am trying my hands on Nestjs and i was a bit confused about the req.user. Where do we get this from and do we need to manually req.user? what actually is req.user and what benefit can we have from it? Do i need to assign payload to it manually?
I have tried searching stackoverflow and nestjs documentaion but did not get a clear insight.
import { createParamDecorator } from '@nestjs/common';
export const User = createParamDecorator((data, req) => req.user);
Like in this example where do i get the req from??
req.user
is nothing but a custom key of req
object.
Which can be inserted from any route by pointing req
object.
But generally, it is inserted from the authorization middleware where we compare user by the token. (JWT).
So req.user
can be accessible in all the root where authorization middleware is called.
Note: You can put any data in it and also with a different key like req.body.yourKey
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