Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is req.user and where is it populated from?

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??


1 Answers

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

like image 76
Hidayt Rahman Avatar answered Sep 07 '25 22:09

Hidayt Rahman