111 lines
4.2 KiB
Plaintext
111 lines
4.2 KiB
Plaintext
@page "/admin/login"
|
|
@layout TaxBaik.Web.Components.Admin.Layout.BlankLayout
|
|
@attribute [AllowAnonymous]
|
|
@rendermode @(new InteractiveServerRenderMode(prerender: true))
|
|
@inject IApiClient ApiClient
|
|
@inject ILocalStorageService LocalStorageService
|
|
@inject IJSRuntime Js
|
|
|
|
<PageTitle>로그인</PageTitle>
|
|
|
|
<MudContainer MaxWidth="MaxWidth.Small" Class="admin-login-page d-flex align-center justify-center" Style="min-height: 100vh;">
|
|
<MudPaper Class="pa-8" Elevation="3" Style="width: 100%; max-width: 400px;">
|
|
<MudText Typo="Typo.h4" Class="mb-6 text-center">관리자 로그인</MudText>
|
|
|
|
<EditForm Model="model" OnValidSubmit="HandleLogin">
|
|
<DataAnnotationsValidator />
|
|
<input class="mud-input mud-input-outlined mud-input-root mud-input-root-adorned-start mb-4"
|
|
style="width: 100%; min-height: 56px; padding: 16px 14px;"
|
|
placeholder="사용자명"
|
|
autocomplete="username"
|
|
@bind="model.Username" />
|
|
|
|
<input type="password"
|
|
class="mud-input mud-input-outlined mud-input-root mud-input-root-adorned-start mb-4"
|
|
style="width: 100%; min-height: 56px; padding: 16px 14px;"
|
|
placeholder="비밀번호"
|
|
autocomplete="current-password"
|
|
@bind="model.Password" />
|
|
|
|
<div class="mb-4">
|
|
<input class="mud-checkbox" type="checkbox" @bind="model.RememberMe" />
|
|
<label style="margin-left: 8px; cursor: pointer;">아이디 저장</label>
|
|
</div>
|
|
|
|
<div class="mud-alert mud-alert-filled-error mb-4 login-error-message" style="display:none;">로그인 중 오류가 발생했습니다.</div>
|
|
|
|
<button type="submit"
|
|
class="mud-button-root mud-button mud-button-filled mud-button-filled-primary mud-elevation-0"
|
|
style="width: 100%; min-height: 52px; border: 0; border-radius: 4px; color: white;">
|
|
<span>로그인</span>
|
|
</button>
|
|
</EditForm>
|
|
</MudPaper>
|
|
</MudContainer>
|
|
|
|
@code {
|
|
private LoginModel model = new();
|
|
private const string RememberedUsernameKey = "admin-remembered-username";
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
try
|
|
{
|
|
var remembered = await LocalStorageService.GetItemAsStringAsync(RememberedUsernameKey);
|
|
if (!string.IsNullOrEmpty(remembered))
|
|
{
|
|
model.Username = remembered;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// LocalStorage may be unavailable during prerender.
|
|
}
|
|
}
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
if (firstRender)
|
|
await Js.InvokeVoidAsync("taxbaikAdminSession.syncRouteClass");
|
|
}
|
|
|
|
private async Task HandleLogin()
|
|
{
|
|
var request = new { model.Username, model.Password };
|
|
var response = await ApiClient.PostAsync<LoginResponse>("auth/login", request);
|
|
|
|
if (response?.AccessToken == null || response?.RefreshToken == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (model.RememberMe)
|
|
{
|
|
await LocalStorageService.SetItemAsStringAsync(RememberedUsernameKey, model.Username);
|
|
}
|
|
else
|
|
{
|
|
await LocalStorageService.RemoveItemAsync(RememberedUsernameKey);
|
|
}
|
|
|
|
await Js.InvokeVoidAsync("localStorage.setItem", "accessToken", response.AccessToken);
|
|
await Js.InvokeVoidAsync("localStorage.setItem", "refreshToken", response.RefreshToken);
|
|
await Js.InvokeVoidAsync("localStorage.setItem", "tokenExpiry", DateTimeOffset.UtcNow.AddSeconds(response.ExpiresIn).ToUnixTimeMilliseconds().ToString());
|
|
await Js.InvokeVoidAsync("window.location.assign", "/taxbaik/admin/dashboard");
|
|
}
|
|
|
|
private class LoginModel
|
|
{
|
|
public string Username { get; set; } = "";
|
|
public string Password { get; set; } = "";
|
|
public bool RememberMe { get; set; }
|
|
}
|
|
|
|
private class LoginResponse
|
|
{
|
|
public string AccessToken { get; set; } = "";
|
|
public string RefreshToken { get; set; } = "";
|
|
public int ExpiresIn { get; set; }
|
|
}
|
|
}
|