question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

retrieve user context from service layer

See original GitHub issue

How can I directly retrieve user information in my service layer?

// Something like that:
const user = getSecurityContext().authenticatedUser()

If I am on the Controller Layer I would use:

@Post()
  create(@AuthenticatedUser() user: any, @Body() createArticleDto: CreateArticleDto) {
    this.logger.debug(`create user now`, user);
    return this.articlesService.create(user, createArticleDto);
  }

But then I need to pass it down to all my services…

Is there a better way to do it?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
deeeedcommented, Jan 15, 2022

Thanks for the additional code and sorry if I wasn’t clear.

I do have access to the user instance inside the Controller layer because AuthGuard is in my module. I was just trying to avoid passing down the user from Controller Layer to Service Layer. Having access to the request makes it possible to parse the token in the Service Layer (as per provided code) but it is inefficient since it was already parsed in the controller layer and I thought there would be another way to re-use it.

I will read more on cls-hooked.

0reactions
ferrerojoshcommented, Jan 17, 2022

Thanks for the additional code and sorry if I wasn’t clear.

I do have access to the user instance inside the Controller layer because AuthGuard is in my module. I was just trying to avoid passing down the user from Controller Layer to Service Layer. Having access to the request makes it possible to parse the token in the Service Layer (as per provided code) but it is inefficient since it was already parsed in the controller layer and I thought there would be another way to re-use it.

I will read more on cls-hooked.

Here’s an interesting gist if you like: https://gist.github.com/bengry/924a9b93c25d8a98bffdfc0a847f0dbe

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get User at Service Layer - Stack Overflow
HttpContext.RequestServices; var sessionProvider = serviceProvider.GetService<SessionProvider>(); var userManager = serviceProvider.GetService< ...
Read more >
asp.net core - get User at Service Layer
1 Answer 1 ; SessionFilter : IAsyncActionFilter ; var session = services.GetService(typeof ; var userIdClaim = claimsIdentity.Claims.
Read more >
Access HttpContext in ASP.NET Core - Microsoft Learn
Context property on the view. The following example retrieves the current username in an intranet app using Windows Authentication:.
Read more >
How to access HttpContext inside the service class in .Net Core
Most of the times we need to access user details and some other context data in service layer after watching this video I...
Read more >
Sending current user to service layer - aka 'Passed in ... - GitHub
Create class which provides (get/set) Request Specific data (like ClaimsPrincipal) and via constructor injection on controller-lower levels ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found