2025-09-28 05:36:45 +03:30
|
|
|
|
using Blazored.LocalStorage;
|
2025-09-28 04:00:06 +03:30
|
|
|
|
using FrontOffice.Main.Utilities;
|
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
using Microsoft.AspNetCore.Components.Routing;
|
|
|
|
|
|
|
|
|
|
|
|
namespace FrontOffice.Main;
|
|
|
|
|
|
|
|
|
|
|
|
public partial class App
|
|
|
|
|
|
{
|
|
|
|
|
|
private const string TokenStorageKey = "auth:token";
|
|
|
|
|
|
|
|
|
|
|
|
[Inject] private ILocalStorageService LocalStorage { get; set; } = default!;
|
|
|
|
|
|
|
|
|
|
|
|
private async Task HandleNavigationAsync(NavigationContext context)
|
|
|
|
|
|
{
|
2025-09-28 05:36:45 +03:30
|
|
|
|
var normalizedPath = NormalizePath(context.Path);
|
|
|
|
|
|
if (IsAuthPath(normalizedPath))
|
2025-09-28 04:00:06 +03:30
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var token = await LocalStorage.GetItemAsync<string>(TokenStorageKey);
|
2025-09-28 05:36:45 +03:30
|
|
|
|
if (!string.IsNullOrWhiteSpace(token))
|
2025-09-28 04:00:06 +03:30
|
|
|
|
{
|
2025-09-28 05:36:45 +03:30
|
|
|
|
return;
|
2025-09-28 04:00:06 +03:30
|
|
|
|
}
|
2025-09-28 05:36:45 +03:30
|
|
|
|
|
|
|
|
|
|
var redirect = string.IsNullOrEmpty(normalizedPath) || normalizedPath == "/"
|
|
|
|
|
|
? string.Empty
|
|
|
|
|
|
: $"?redirect={Uri.EscapeDataString(normalizedPath)}";
|
|
|
|
|
|
|
|
|
|
|
|
Navigation.NavigateTo(RouteConstants.Auth.Phone + redirect, forceLoad: true);
|
2025-09-28 04:00:06 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static bool IsAuthPath(string? path)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 05:36:45 +03:30
|
|
|
|
if (Uri.TryCreate(path, UriKind.Absolute, out var absolute))
|
|
|
|
|
|
{
|
|
|
|
|
|
path = absolute.PathAndQuery;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 04:00:06 +03:30
|
|
|
|
path = path.TrimStart('/');
|
|
|
|
|
|
return path.StartsWith("auth", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string NormalizePath(string? path)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 05:36:45 +03:30
|
|
|
|
if (Uri.TryCreate(path, UriKind.Absolute, out var absolute))
|
|
|
|
|
|
{
|
|
|
|
|
|
path = absolute.PathAndQuery;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return path.StartsWith('/') ? path : "/" + path;
|
2025-09-28 04:00:06 +03:30
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-28 05:36:45 +03:30
|
|
|
|
|