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

60
src/App.razor.cs Normal file
View File

@@ -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<string>(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;
}
}

View File

@@ -1,4 +1,7 @@
<Router AppAssembly="@typeof(App).Assembly">
@using FrontOffice.Main.Utilities
<Router AppAssembly="@typeof(App).Assembly"
OnNavigateAsync="HandleNavigationAsync">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>

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;
}
}

View File

@@ -1,4 +1,4 @@
@page "/"
@page "/"
@using Microsoft.AspNetCore.Components.Web
@namespace FrontOffice.Main.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@@ -12,26 +12,27 @@
<link href="css/site.css" rel="stylesheet" />
<link href="FrontOffice.Main.styles.css" rel="stylesheet" />
<link rel="icon" type="image/png" href="favicon.png"/>
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
<component type="typeof(HeadOutlet)" render-mode="Server" />
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
</head>
<body>
<component type="typeof(App)" render-mode="ServerPrerendered" />
<component type="typeof(App)" render-mode="Server" />
<div id="blazor-error-ui">
<environment include="Staging,Production">
یک خطا رخ داده است. این برنامه ممکن است تا زمان بارگذاری مجدد پاسخگو نباشد.
?? ??? ?? ???? ???. ??? ?????? ???? ??? ?? ???? ???????? ???? ?????? ?????.
</environment>
<environment include="Development">
خطای مدیریت‌نشده ای اتفاق افتاده است. برای مشاهده جزئیات، ابزار توسعه (DevTools) مرورگر را بررسی نمایید.
???? ??????????? ?? ????? ?????? ???. ???? ?????? ??????? ????? ????? (DevTools) ?????? ?? ????? ??????.
</environment>
<a href="" class="reload">بارگذاری مجدد</a>
<a class="dismiss">🗙</a>
<a href="" class="reload">???????? ????</a>
<a class="dismiss">??</a>
</div>
<script src="_framework/blazor.server.js"></script>
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
</body>
</html>