This commit is contained in:
MeysamMoghaddam
2025-09-28 04:00:06 +03:30
parent f9567c4265
commit db94bd337f
4 changed files with 129 additions and 8 deletions

View File

@@ -0,0 +1,57 @@
using System;
using System.Threading.Tasks;
using Blazored.LocalStorage;
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)
{
var relativePath = Navigation.ToBaseRelativePath(context.Path);
if (IsAuthPath(relativePath))
{
return;
}
var token = await LocalStorage.GetItemAsync<string>(TokenStorageKey);
if (string.IsNullOrWhiteSpace(token))
{
var normalized = NormalizePath(relativePath);
var redirectQuery = string.IsNullOrEmpty(normalized) || normalized == "/"
? string.Empty
: $"?redirect={Uri.EscapeDataString(normalized)}";
Navigation.NavigateTo(RouteConstants.Auth.Phone + redirectQuery, forceLoad: true);
}
}
private static bool IsAuthPath(string? path)
{
if (string.IsNullOrWhiteSpace(path))
{
return false;
}
path = path.TrimStart('/');
return path.StartsWith("auth", StringComparison.OrdinalIgnoreCase);
}
private static string NormalizePath(string? path)
{
if (string.IsNullOrWhiteSpace(path))
{
return string.Empty;
}
var normalized = path.StartsWith('/') ? path : "/" + path;
return normalized;
}
}