Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] How to add userId for each authenticated request #433

Closed
vladiulianbogdan opened this issue Feb 26, 2021 · 5 comments
Closed
Assignees
Labels
question Further information is requested

Comments

@vladiulianbogdan
Copy link

Hello,

I am using your library in my project. Thanks for your work, it is really helpful!

We are using a Passport local strategy for authentication (https://docs.nestjs.com/security/authentication#implementing-passport-local). If the authentication is performed correctly, the user object will be added on the request object that it will be later passed to the controllers.

What I would like to do is to add the userId value to each request information that is bundled together with each log. It seems that the serializer methods are called before this auth layer is called and I did not find a way to do this in the documentation.

Do you have any advice on how to implement this?

Thank you!

@vladiulianbogdan vladiulianbogdan added the question Further information is requested label Feb 26, 2021
@iamolegga
Copy link
Owner

Hi, for such purpose I've created this library, because nest doesn't have guarantee of modules registration and middlewares order, you can see discussion for same problem of middlewares order in another my package. All you need is wrap this module in another one with extra decorator, and add one to your passport/auth/whatever module.

and the second part is you need to find specific option to add new properties to logs

Feel free to ping me with your code exmaples

@EdByrnee
Copy link

Did you manage to find a solution for this?

@vladiulianbogdan
Copy link
Author

I haven't tried yet what iamolegga suggested, but I will do it as soon as possible and I will let you know.

@vladiulianbogdan
Copy link
Author

I was unable to do it with nestjs-configure-after because in our system the authentication is not performed with a middleware, but with a decorator on top of the controller method. So, if I understand correctly, the logging middleware will catch and store the request before the decorator in the controller is called.

My solution to this problem is to use the logger.assign (https://github.com/iamolegga/nestjs-pino#assign-extra-fields-for-future-calls) method to add the user id information to the log.

@MikeKoval
Copy link

@vladiulianbogdan can you share snippet how you resolved that issue with decorators pls?
do you use logger.assign inside decorator or at controller itself? if in decorator, how do you pass it there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants