diff --git a/src/FrontOffice.Main/ConfigureServices.cs b/src/FrontOffice.Main/ConfigureServices.cs index d240d33..7a67069 100644 --- a/src/FrontOffice.Main/ConfigureServices.cs +++ b/src/FrontOffice.Main/ConfigureServices.cs @@ -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(); + services.AddScoped(); + return services; } diff --git a/src/FrontOffice.Main/Pages/Profile/Components/OrganizationChartLevel.razor.cs b/src/FrontOffice.Main/Pages/Profile/Components/OrganizationChartLevel.razor.cs index 4500b89..579fd09 100644 --- a/src/FrontOffice.Main/Pages/Profile/Components/OrganizationChartLevel.razor.cs +++ b/src/FrontOffice.Main/Pages/Profile/Components/OrganizationChartLevel.razor.cs @@ -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 diff --git a/src/FrontOffice.Main/Shared/MainLayout.razor b/src/FrontOffice.Main/Shared/MainLayout.razor index 6b2ef51..336f588 100644 --- a/src/FrontOffice.Main/Shared/MainLayout.razor +++ b/src/FrontOffice.Main/Shared/MainLayout.razor @@ -41,20 +41,18 @@
- - @if (_isAuthenticated) - { - - پروفایل - - خروج از حساب - - } - else - { - ورود - } - + @if (_isAuthenticated) + { + + پروفایل + + خروج از حساب + + } + else + { + + } diff --git a/src/FrontOffice.Main/Shared/MainLayout.razor.cs b/src/FrontOffice.Main/Shared/MainLayout.razor.cs index 67fb1e7..dfa3ecc 100644 --- a/src/FrontOffice.Main/Shared/MainLayout.razor.cs +++ b/src/FrontOffice.Main/Shared/MainLayout.razor.cs @@ -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(TokenStorageKey); - _isAuthenticated = !string.IsNullOrWhiteSpace(token); + _isAuthenticated = await AuthService.IsAuthenticatedAsync(); } private async Task OpenAuthDialog() { - var dialog = await DialogService.ShowAsync("ورود به حساب کاربری"); - var result = await dialog.Result; - - if (!result.Canceled) + await AuthDialogService.ShowAuthDialogAsync(); + await CheckAuthStatus(); + if (_isAuthenticated) { - await CheckAuthStatus(); 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); } } \ No newline at end of file diff --git a/src/FrontOffice.Main/Utilities/AuthDialogService.cs b/src/FrontOffice.Main/Utilities/AuthDialogService.cs new file mode 100644 index 0000000..32532e7 --- /dev/null +++ b/src/FrontOffice.Main/Utilities/AuthDialogService.cs @@ -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("ورود به حساب کاربری"); + var result = await dialog.Result; + + if (!result.Canceled) + { + // User logged in successfully + // You can add additional logic here if needed + } + } +} \ No newline at end of file diff --git a/src/FrontOffice.Main/Utilities/AuthService.cs b/src/FrontOffice.Main/Utilities/AuthService.cs new file mode 100644 index 0000000..004dfe3 --- /dev/null +++ b/src/FrontOffice.Main/Utilities/AuthService.cs @@ -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 IsAuthenticatedAsync() + { + var token = await _localStorage.GetItemAsync(TokenStorageKey); + return !string.IsNullOrWhiteSpace(token); + } + + public async Task GetTokenAsync() + { + return await _localStorage.GetItemAsync(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); + } + } +} \ No newline at end of file