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

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