mirror of
https://github.com/Shchoholiev/shopping-assistant-web-client.git
synced 2025-04-04 16:49:36 +00:00
38 lines
1.3 KiB
C#
38 lines
1.3 KiB
C#
using ShoppingAssistantWebClient.Web.Models.GlobalInstances;
|
|
using ShoppingAssistantWebClient.Web.Network;
|
|
using System.Security.Authentication;
|
|
|
|
namespace ShoppingAssistantWebClient.Web.CustomMiddlewares;
|
|
|
|
public class GlobalUserMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
|
|
public GlobalUserMiddleware(RequestDelegate next)
|
|
{
|
|
this._next = next;
|
|
}
|
|
|
|
public async Task InvokeAsync(HttpContext httpContext, AuthenticationService authenticationService, ApiClient apiClient)
|
|
{
|
|
try
|
|
{
|
|
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("");
|
|
}
|
|
|
|
await _next(httpContext);
|
|
}
|
|
} |