admin管理员组

文章数量:1402783

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/mon';

export const User = createParamDecorator((data, req) => req.user);

Like in this example where do i get the req 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/mon';

export const User = createParamDecorator((data, req) => req.user);

Like in this example where do i get the req from??

Share Improve this question asked Mar 29, 2021 at 4:14 user15188165user15188165 1
  • this is an old API (nestjs v6). Please read this: docs.nestjs./custom-decorators and this migration guide – Micael Levi Commented Mar 29, 2021 at 4:20
Add a ment  | 

1 Answer 1

Reset to default 5

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

本文标签: javascriptwhat is requser and where is it populated fromStack Overflow