58 lines
1.6 KiB
C#
58 lines
1.6 KiB
C#
|
|
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;
|
|||
|
|
}
|
|||
|
|
}
|