From c5edc8df79119051f4952689648cb744bbbe92de Mon Sep 17 00:00:00 2001 From: Mykhailo Bilodid Date: Wed, 1 Nov 2023 17:34:30 +0200 Subject: [PATCH] SA-142 login loop fixed --- .../DependencyInjectionExtension.cs | 2 +- .../CustomMiddlewares/GlobalUserMiddleware.cs | 29 ++++++++++--------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/ShoppingAssistantWebClient.Web/Configurations/DependencyInjectionExtension.cs b/ShoppingAssistantWebClient.Web/Configurations/DependencyInjectionExtension.cs index b50cb1d..1d73548 100644 --- a/ShoppingAssistantWebClient.Web/Configurations/DependencyInjectionExtension.cs +++ b/ShoppingAssistantWebClient.Web/Configurations/DependencyInjectionExtension.cs @@ -15,7 +15,7 @@ public static class DependencyInjectionExtension client.BaseAddress = new Uri(apiUrl + "api/"); }); - services.AddScoped(p => + services.AddSingleton(p => new GraphQLHttpClient(apiUrl + "graphql", new NewtonsoftJsonSerializer()) ); diff --git a/ShoppingAssistantWebClient.Web/CustomMiddlewares/GlobalUserMiddleware.cs b/ShoppingAssistantWebClient.Web/CustomMiddlewares/GlobalUserMiddleware.cs index 5606a2b..91f5823 100644 --- a/ShoppingAssistantWebClient.Web/CustomMiddlewares/GlobalUserMiddleware.cs +++ b/ShoppingAssistantWebClient.Web/CustomMiddlewares/GlobalUserMiddleware.cs @@ -15,22 +15,25 @@ public class GlobalUserMiddleware public async Task InvokeAsync(HttpContext httpContext, AuthenticationService authenticationService, ApiClient apiClient) { - try + if (httpContext.Request.Path != "/login") { - var accessToken = await authenticationService.GetAuthTokenAsync(); - if (!string.IsNullOrEmpty(accessToken)) + try { - apiClient.JwtToken = accessToken; - GlobalUser.Roles = authenticationService.GetRolesFromJwtToken(accessToken); - GlobalUser.Id = authenticationService.GetIdFromJwtToken(accessToken); - GlobalUser.Email = authenticationService.GetEmailFromJwtToken(accessToken); - GlobalUser.Phone = authenticationService.GetPhoneFromJwtToken(accessToken); + var accessToken = await authenticationService.GetAuthTokenAsync(); + if (!string.IsNullOrEmpty(accessToken)) + { + apiClient.JwtToken = accessToken; + GlobalUser.Roles = authenticationService.GetRolesFromJwtToken(accessToken); + GlobalUser.Id = authenticationService.GetIdFromJwtToken(accessToken); + GlobalUser.Email = authenticationService.GetEmailFromJwtToken(accessToken); + GlobalUser.Phone = authenticationService.GetPhoneFromJwtToken(accessToken); + } + } + catch (AuthenticationException ex) + { + httpContext.Response.Cookies.Delete("accessToken"); + httpContext.Response.Redirect("/login"); } - } - catch (AuthenticationException ex) - { - httpContext.Response.Cookies.Delete("accessToken"); - httpContext.Response.Redirect("/login"); } await _next(httpContext); }