SA-142 login loop fixed

This commit is contained in:
Mykhailo Bilodid 2023-11-01 17:34:30 +02:00
parent 7493d3906b
commit c5edc8df79
2 changed files with 17 additions and 14 deletions

View File

@ -15,7 +15,7 @@ public static class DependencyInjectionExtension
client.BaseAddress = new Uri(apiUrl + "api/"); client.BaseAddress = new Uri(apiUrl + "api/");
}); });
services.AddScoped<GraphQLHttpClient>(p => services.AddSingleton<GraphQLHttpClient>(p =>
new GraphQLHttpClient(apiUrl + "graphql", new NewtonsoftJsonSerializer()) new GraphQLHttpClient(apiUrl + "graphql", new NewtonsoftJsonSerializer())
); );

View File

@ -15,22 +15,25 @@ public class GlobalUserMiddleware
public async Task InvokeAsync(HttpContext httpContext, AuthenticationService authenticationService, ApiClient apiClient) public async Task InvokeAsync(HttpContext httpContext, AuthenticationService authenticationService, ApiClient apiClient)
{ {
try if (httpContext.Request.Path != "/login")
{ {
var accessToken = await authenticationService.GetAuthTokenAsync(); try
if (!string.IsNullOrEmpty(accessToken))
{ {
apiClient.JwtToken = accessToken; var accessToken = await authenticationService.GetAuthTokenAsync();
GlobalUser.Roles = authenticationService.GetRolesFromJwtToken(accessToken); if (!string.IsNullOrEmpty(accessToken))
GlobalUser.Id = authenticationService.GetIdFromJwtToken(accessToken); {
GlobalUser.Email = authenticationService.GetEmailFromJwtToken(accessToken); apiClient.JwtToken = accessToken;
GlobalUser.Phone = authenticationService.GetPhoneFromJwtToken(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); await _next(httpContext);
} }