using System.Security.Claims; using Microsoft.AspNetCore.Components.Authorization; namespace TaxBaik.Admin.Services; public class CustomAuthenticationStateProvider : AuthenticationStateProvider { private readonly ILocalStorageService _localStorage; private readonly AuthService _authService; private readonly ILogger _logger; public CustomAuthenticationStateProvider(ILocalStorageService localStorage, AuthService authService, ILogger logger) { _localStorage = localStorage; _authService = authService; _logger = logger; } public override async Task GetAuthenticationStateAsync() { try { var token = await _localStorage.GetItemAsStringAsync("auth_token"); if (string.IsNullOrEmpty(token)) { return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())); } var principal = _authService.ValidateToken(token); if (principal == null) { await _localStorage.RemoveItemAsync("auth_token"); return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())); } return new AuthenticationState(principal); } catch (Exception ex) { _logger.LogError(ex, "인증 상태 조회 중 오류 발생"); return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())); } } public async Task LoginAsync(string token) { await _localStorage.SetItemAsStringAsync("auth_token", token); var principal = _authService.ValidateToken(token); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } public async Task LogoutAsync() { await _localStorage.RemoveItemAsync("auth_token"); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } }