diff --git a/src/App.razor.cs b/src/App.razor.cs new file mode 100644 index 0000000..032d529 --- /dev/null +++ b/src/App.razor.cs @@ -0,0 +1,60 @@ +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!; + [Inject] private NavigationManager Navigation { get; set; } = default!; + + private async Task HandleNavigationAsync(NavigationContext context) + { + var relativePath = Navigation.ToBaseRelativePath(context.Path); + if (IsAuthPath(relativePath)) + { + return; + } + + var token = await LocalStorage.GetItemAsync(TokenStorageKey); + if (string.IsNullOrWhiteSpace(token)) + { + context.PreventNavigation(); + + 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; + } +} diff --git a/src/FrontOffice.Main/App.razor b/src/FrontOffice.Main/App.razor index d414dad..936967d 100644 --- a/src/FrontOffice.Main/App.razor +++ b/src/FrontOffice.Main/App.razor @@ -1,4 +1,7 @@ - +@using FrontOffice.Main.Utilities + + diff --git a/src/FrontOffice.Main/App.razor.cs b/src/FrontOffice.Main/App.razor.cs new file mode 100644 index 0000000..50432ad --- /dev/null +++ b/src/FrontOffice.Main/App.razor.cs @@ -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(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; + } +} diff --git a/src/FrontOffice.Main/Pages/_Host.cshtml b/src/FrontOffice.Main/Pages/_Host.cshtml index bca9463..34486e9 100644 --- a/src/FrontOffice.Main/Pages/_Host.cshtml +++ b/src/FrontOffice.Main/Pages/_Host.cshtml @@ -1,4 +1,4 @@ -@page "/" +@page "/" @using Microsoft.AspNetCore.Components.Web @namespace FrontOffice.Main.Pages @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @@ -12,26 +12,27 @@ - + - +
- یک خطا رخ داده است. این برنامه ممکن است تا زمان بارگذاری مجدد پاسخگو نباشد. + ?? ??? ?? ???? ???. ??? ?????? ???? ??? ?? ???? ???????? ???? ?????? ?????. - خطای مدیریت‌نشده ای اتفاق افتاده است. برای مشاهده جزئیات، ابزار توسعه (DevTools) مرورگر را بررسی نمایید. + ???? ??????????? ?? ????? ?????? ???. ???? ?????? ??????? ????? ????? (DevTools) ?????? ?? ????? ??????. - بارگذاری مجدد - 🗙 + ???????? ???? + ??
+