This commit is contained in:
MeysamMoghaddam
2025-10-20 21:17:53 +03:30
parent 59e58abaf9
commit d69a3617d9
6 changed files with 109 additions and 32 deletions

View File

@@ -10,7 +10,7 @@ using FrontOffice.BFF.Transaction.Protobuf.Protos.Transaction;
using FrontOffice.BFF.User.Protobuf.Protos.User;
using FrontOffice.BFF.UserAddress.Protobuf.Protos.UserAddress;
using FrontOffice.BFF.UserOrder.Protobuf.Protos.UserOrder;
using FrontOffice.BFF.Transaction.Protobuf.Protos.Transaction;
using FrontOffice.Main.Utilities;
namespace Microsoft.Extensions.DependencyInjection;
@@ -29,6 +29,11 @@ public static class ConfigureServices
config.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip;
config.JsonSerializerOptions.WriteIndented = false;
});
// Register custom services
services.AddScoped<AuthService>();
services.AddScoped<AuthDialogService>();
return services;
}

View File

@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MudBlazor;
using FrontOffice.Main.Utilities;
namespace FrontOffice.Main.Pages.Profile.Components;
public partial class OrganizationChartLevel
@@ -10,6 +11,8 @@ public partial class OrganizationChartLevel
[CascadingParameter] private OrganizationChart? ParentChart { get; set; }
[Inject] private AuthDialogService AuthDialogService { get; set; } = default!;
private Size GetAvatarSize(int level)
{
return level switch

View File

@@ -41,7 +41,6 @@
</div>
<div class="d-flex align-center gap-2">
<MudHidden Breakpoint="Breakpoint.SmAndUp" Invert="true">
@if (_isAuthenticated)
{
<MudMenu Icon="@Icons.Material.Filled.Person" Color="Color.Inherit" Size="Size.Medium">
@@ -52,9 +51,8 @@
}
else
{
<MudButton Variant="Variant.Outlined" Color="Color.Inherit" OnClick="OpenAuthDialog">ورود</MudButton>
<MudIconButton Icon="@Icons.Material.Filled.Login" Color="Color.Inherit" OnClick="OpenAuthDialog" />
}
</MudHidden>
<MudIconButton OnClick="@ToggleTheme" Edge="Edge.End"
Icon="@(_isDark ? Icons.Material.Filled.DarkMode : Icons.Material.Filled.LightMode)" />

View File

@@ -3,6 +3,7 @@ using FrontOffice.Main.Utilities;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MudBlazor;
using Microsoft.AspNetCore.Components.Authorization;
namespace FrontOffice.Main.Shared;
public partial class MainLayout
@@ -16,6 +17,8 @@ public partial class MainLayout
private string? _email;
[Inject] private ILocalStorageService LocalStorage { get; set; } = default!;
[Inject] private AuthService AuthService { get; set; } = default!;
[Inject] private AuthDialogService AuthDialogService { get; set; } = default!;
private void ToggleTheme() => _isDark = !_isDark;
private void ToggleDrawer() => _drawerOpen = !_drawerOpen;
@@ -36,21 +39,18 @@ public partial class MainLayout
private async Task CheckAuthStatus()
{
var token = await LocalStorage.GetItemAsync<string>(TokenStorageKey);
_isAuthenticated = !string.IsNullOrWhiteSpace(token);
_isAuthenticated = await AuthService.IsAuthenticatedAsync();
}
private async Task OpenAuthDialog()
{
var dialog = await DialogService.ShowAsync<AuthDialog>("ورود به حساب کاربری");
var result = await dialog.Result;
if (!result.Canceled)
{
await AuthDialogService.ShowAuthDialogAsync();
await CheckAuthStatus();
if (_isAuthenticated)
{
Snackbar.Add(GlobalConstants.SuccessMsg, Severity.Success);
StateHasChanged();
}
StateHasChanged();
}
private void NavigateToProfile()
@@ -60,16 +60,8 @@ public partial class MainLayout
private async Task Logout()
{
await LocalStorage.RemoveItemAsync(TokenStorageKey);
await LocalStorage.RemoveItemAsync("auth:phone-number");
await LocalStorage.RemoveItemAsync("auth:redirect");
await LocalStorage.RemoveItemAsync("referral:code");
await AuthService.LogoutAsync();
_isAuthenticated = false;
Snackbar.Add("با موفقیت از حساب کاربری خارج شدید.", Severity.Success);
StateHasChanged();
// Navigate to home page
Navigation.NavigateTo(RouteConstants.Main.MainPage);
}
}

View File

@@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Components;
using MudBlazor;
namespace FrontOffice.Main.Utilities;
public class AuthDialogService
{
private readonly IDialogService _dialogService;
public AuthDialogService(IDialogService dialogService)
{
_dialogService = dialogService;
}
public async Task ShowAuthDialogAsync()
{
var dialog = await _dialogService.ShowAsync<Shared.AuthDialog>("ورود به حساب کاربری");
var result = await dialog.Result;
if (!result.Canceled)
{
// User logged in successfully
// You can add additional logic here if needed
}
}
}

View File

@@ -0,0 +1,53 @@
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components;
using MudBlazor;
namespace FrontOffice.Main.Utilities;
public class AuthService
{
private readonly ILocalStorageService _localStorage;
private readonly NavigationManager _navigation;
private readonly ISnackbar _snackbar;
private const string TokenStorageKey = "auth:token";
public AuthService(ILocalStorageService localStorage, NavigationManager navigation, ISnackbar snackbar)
{
_localStorage = localStorage;
_navigation = navigation;
_snackbar = snackbar;
}
public async Task<bool> IsAuthenticatedAsync()
{
var token = await _localStorage.GetItemAsync<string>(TokenStorageKey);
return !string.IsNullOrWhiteSpace(token);
}
public async Task<string?> GetTokenAsync()
{
return await _localStorage.GetItemAsync<string>(TokenStorageKey);
}
public async Task LogoutAsync()
{
await _localStorage.RemoveItemAsync(TokenStorageKey);
await _localStorage.RemoveItemAsync("auth:phone-number");
await _localStorage.RemoveItemAsync("auth:redirect");
await _localStorage.RemoveItemAsync("referral:code");
_snackbar.Add("با موفقیت از حساب کاربری خارج شدید.", Severity.Success);
_navigation.NavigateTo(RouteConstants.Main.MainPage);
}
public async Task RequireAuthenticationAsync()
{
var isAuthenticated = await IsAuthenticatedAsync();
if (!isAuthenticated)
{
_snackbar.Add("لطفاً ابتدا وارد حساب کاربری شوید.", Severity.Warning);
_navigation.NavigateTo(RouteConstants.Main.MainPage);
}
}
}