| | 1 | | using System.Net; |
| | 2 | | using Microsoft.JSInterop; |
| | 3 | | using Microsoft.AspNetCore.Components; |
| | 4 | | using Microsoft.AspNetCore.Components.WebAssembly.Http; |
| | 5 | |
|
| | 6 | | namespace Syki.Front.Auth; |
| | 7 | |
|
| 0 | 8 | | public class SykiDelegatingHandler(ILocalStorageService storage, NavigationManager nav, SykiAuthStateProvider auth) : De |
| | 9 | | { |
| | 10 | | protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellat |
| | 11 | | { |
| 0 | 12 | | request.SetBrowserRequestCredentials(BrowserRequestCredentials.Include); |
| | 13 | |
|
| 0 | 14 | | var token = await storage.GetItemAsync("AccessToken"); |
| | 15 | |
|
| 0 | 16 | | if (token != null) |
| | 17 | | { |
| 0 | 18 | | request.Headers.Add("Authorization", $"Bearer {token}"); |
| | 19 | | } |
| | 20 | |
|
| 0 | 21 | | var response = await base.SendAsync(request, cancellationToken); |
| | 22 | |
|
| 0 | 23 | | if (response.StatusCode == HttpStatusCode.Unauthorized) |
| | 24 | | { |
| 0 | 25 | | await storage.RemoveItemAsync("AccessToken"); |
| 0 | 26 | | auth.MarkUserAsLoggedOut(); |
| 0 | 27 | | if (!nav.Uri.Equals("/")) |
| 0 | 28 | | nav.NavigateTo("/", forceLoad: true); |
| | 29 | | } |
| | 30 | |
|
| 0 | 31 | | response.Headers.TryGetValues("X-SkipUserRegister", out var skipUserRegister); |
| 0 | 32 | | await storage.SetItemAsync("SkipUserRegister", skipUserRegister?.FirstOrDefault() ?? "False"); |
| | 33 | |
|
| 0 | 34 | | response.Headers.TryGetValues("X-CrossLogin", out var crossLogin); |
| 0 | 35 | | await storage.SetItemAsync("CrossLogin", crossLogin?.FirstOrDefault() ?? "False"); |
| | 36 | |
|
| 0 | 37 | | return response; |
| 0 | 38 | | } |
| | 39 | | } |