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

View File

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

View File

@@ -41,20 +41,18 @@
</div> </div>
<div class="d-flex align-center gap-2"> <div class="d-flex align-center gap-2">
<MudHidden Breakpoint="Breakpoint.SmAndUp" Invert="true"> @if (_isAuthenticated)
@if (_isAuthenticated) {
{ <MudMenu Icon="@Icons.Material.Filled.Person" Color="Color.Inherit" Size="Size.Medium">
<MudMenu Icon="@Icons.Material.Filled.Person" Color="Color.Inherit" Size="Size.Medium"> <MudMenuItem OnClick="NavigateToProfile">پروفایل</MudMenuItem>
<MudMenuItem OnClick="NavigateToProfile">پروفایل</MudMenuItem> <MudDivider />
<MudDivider /> <MudMenuItem OnClick="Logout">خروج از حساب</MudMenuItem>
<MudMenuItem OnClick="Logout">خروج از حساب</MudMenuItem> </MudMenu>
</MudMenu> }
} else
else {
{ <MudIconButton Icon="@Icons.Material.Filled.Login" Color="Color.Inherit" OnClick="OpenAuthDialog" />
<MudButton Variant="Variant.Outlined" Color="Color.Inherit" OnClick="OpenAuthDialog">ورود</MudButton> }
}
</MudHidden>
<MudIconButton OnClick="@ToggleTheme" Edge="Edge.End" <MudIconButton OnClick="@ToggleTheme" Edge="Edge.End"
Icon="@(_isDark ? Icons.Material.Filled.DarkMode : Icons.Material.Filled.LightMode)" /> 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.AspNetCore.Components;
using Microsoft.JSInterop; using Microsoft.JSInterop;
using MudBlazor; using MudBlazor;
using Microsoft.AspNetCore.Components.Authorization;
namespace FrontOffice.Main.Shared; namespace FrontOffice.Main.Shared;
public partial class MainLayout public partial class MainLayout
@@ -16,6 +17,8 @@ public partial class MainLayout
private string? _email; private string? _email;
[Inject] private ILocalStorageService LocalStorage { get; set; } = default!; [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 ToggleTheme() => _isDark = !_isDark;
private void ToggleDrawer() => _drawerOpen = !_drawerOpen; private void ToggleDrawer() => _drawerOpen = !_drawerOpen;
@@ -36,21 +39,18 @@ public partial class MainLayout
private async Task CheckAuthStatus() private async Task CheckAuthStatus()
{ {
var token = await LocalStorage.GetItemAsync<string>(TokenStorageKey); _isAuthenticated = await AuthService.IsAuthenticatedAsync();
_isAuthenticated = !string.IsNullOrWhiteSpace(token);
} }
private async Task OpenAuthDialog() private async Task OpenAuthDialog()
{ {
var dialog = await DialogService.ShowAsync<AuthDialog>("ورود به حساب کاربری"); await AuthDialogService.ShowAuthDialogAsync();
var result = await dialog.Result; await CheckAuthStatus();
if (_isAuthenticated)
if (!result.Canceled)
{ {
await CheckAuthStatus();
Snackbar.Add(GlobalConstants.SuccessMsg, Severity.Success); Snackbar.Add(GlobalConstants.SuccessMsg, Severity.Success);
StateHasChanged();
} }
StateHasChanged();
} }
private void NavigateToProfile() private void NavigateToProfile()
@@ -60,16 +60,8 @@ public partial class MainLayout
private async Task Logout() private async Task Logout()
{ {
await LocalStorage.RemoveItemAsync(TokenStorageKey); await AuthService.LogoutAsync();
await LocalStorage.RemoveItemAsync("auth:phone-number");
await LocalStorage.RemoveItemAsync("auth:redirect");
await LocalStorage.RemoveItemAsync("referral:code");
_isAuthenticated = false; _isAuthenticated = false;
Snackbar.Add("با موفقیت از حساب کاربری خارج شدید.", Severity.Success);
StateHasChanged(); 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);
}
}
}