feat: NavMenu 로그아웃 버튼 + 토큰 갱신 로직
1. NavMenu에 사용자명 표시 및 로그아웃 버튼 추가 2. CustomAuthenticationStateProvider에 토큰 만료 검증 추가 3. Routes.razor 간소화 (AuthorizeRouteView 사용) 4. 미인증 사용자는 _Imports.razor의 [Authorize]로 보호됨 테스트 계정: admin / admin123 Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
|
||||
@@ -27,6 +28,13 @@ public class CustomAuthenticationStateProvider : AuthenticationStateProvider
|
||||
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
|
||||
}
|
||||
|
||||
if (IsTokenExpired(token))
|
||||
{
|
||||
_logger.LogWarning("토큰 만료됨");
|
||||
await _localStorage.RemoveItemAsync("auth_token");
|
||||
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
|
||||
}
|
||||
|
||||
var principal = _authService.ValidateToken(token);
|
||||
if (principal == null)
|
||||
{
|
||||
@@ -46,8 +54,6 @@ public class CustomAuthenticationStateProvider : AuthenticationStateProvider
|
||||
public async Task LoginAsync(string token)
|
||||
{
|
||||
await _localStorage.SetItemAsStringAsync("auth_token", token);
|
||||
|
||||
var principal = _authService.ValidateToken(token);
|
||||
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
||||
}
|
||||
|
||||
@@ -56,4 +62,18 @@ public class CustomAuthenticationStateProvider : AuthenticationStateProvider
|
||||
await _localStorage.RemoveItemAsync("auth_token");
|
||||
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
||||
}
|
||||
|
||||
private bool IsTokenExpired(string token)
|
||||
{
|
||||
try
|
||||
{
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var jwtToken = handler.ReadJwtToken(token);
|
||||
return jwtToken.ValidTo < DateTime.UtcNow;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,26 @@
|
||||
<div class="top-row ps-3 navbar navbar-dark">
|
||||
@using System.Security.Claims
|
||||
@using Microsoft.AspNetCore.Components.Authorization
|
||||
@using TaxBaik.Admin.Services
|
||||
@inject AuthenticationStateProvider AuthStateProvider
|
||||
@inject CustomAuthenticationStateProvider CustomAuthStateProvider
|
||||
@inject NavigationManager NavigationManager
|
||||
|
||||
<div class="top-row ps-3 navbar navbar-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="">TaxBaik.Admin</a>
|
||||
<div class="d-flex align-items-center gap-3 ms-auto">
|
||||
<AuthorizeView>
|
||||
<Authorized>
|
||||
<div class="user-info text-light">
|
||||
<span>@context.User.FindFirst(ClaimTypes.Name)?.Value</span>
|
||||
<button class="btn btn-sm btn-outline-light ms-2" @onclick="HandleLogout">로그아웃</button>
|
||||
</div>
|
||||
</Authorized>
|
||||
<NotAuthorized>
|
||||
<a href="login" class="btn btn-sm btn-outline-light">로그인</a>
|
||||
</NotAuthorized>
|
||||
</AuthorizeView>
|
||||
</div>
|
||||
<button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
@@ -10,18 +30,18 @@
|
||||
<div class="@NavMenuCssClass nav-scrollable" @onclick="ToggleNavMenu">
|
||||
<nav class="flex-column">
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
|
||||
<span class="oi oi-home" aria-hidden="true"></span> Home
|
||||
<NavLink class="nav-link" href="dashboard" Match="NavLinkMatch.All">
|
||||
<span class="oi oi-home" aria-hidden="true"></span> 대시보드
|
||||
</NavLink>
|
||||
</div>
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="counter">
|
||||
<span class="oi oi-plus" aria-hidden="true"></span> Counter
|
||||
<NavLink class="nav-link" href="blog">
|
||||
<span class="oi oi-document" aria-hidden="true"></span> 블로그
|
||||
</NavLink>
|
||||
</div>
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="fetchdata">
|
||||
<span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
|
||||
<NavLink class="nav-link" href="inquiries">
|
||||
<span class="oi oi-envelope-closed" aria-hidden="true"></span> 문의사항
|
||||
</NavLink>
|
||||
</div>
|
||||
</nav>
|
||||
@@ -36,4 +56,10 @@
|
||||
{
|
||||
collapseNavMenu = !collapseNavMenu;
|
||||
}
|
||||
|
||||
private async Task HandleLogout()
|
||||
{
|
||||
await CustomAuthStateProvider.LogoutAsync();
|
||||
NavigationManager.NavigateTo("/login", forceLoad: true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,3 +8,4 @@
|
||||
@using Microsoft.JSInterop
|
||||
@using TaxBaik.Admin
|
||||
@using TaxBaik.Admin.Shared
|
||||
@using TaxBaik.Admin.Services
|
||||
|
||||
Reference in New Issue
Block a user