简体   繁体   中英

Where to store user information or any other request context information in nest.js

This is more of a theoretical question than a specific code problem. While using JWT authentication in my nest.js API, after token verification compltes successfully and a user identity is received where do I keep this information in the running request context to be used deep down in my app (maybe in a service or some other class). For example, I need to update some property of a record in the database along with who updated it. This db query could be nested deep into my application with no access to the request object.

Please note that the user identity should be available in context of the current request only and a different request could potentially have a different user information. I read about the execution context on the documentation page of nest but that does not give me any idea.

The usual practice in NodeJS is to attach the user identity to the user property of the request object. This is how passport does it, but be aware that because the Express and Fastify request objects are different, this can lead to some different behaviors between the two adapters. What you would do after adding the identity (usually done in a guard), is in the controller, get the identity from req.user (this can be done by pulling the entire request ( @Request() ) or by creating a custom decorator )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM