retrieve user context from service layer
See original GitHub issueHow 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:
- Created 2 years ago
- Comments:15 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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