chore
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
@layout EmptyLayout
|
||||
@inject ILogger<Login> Logger
|
||||
|
||||
|
||||
<PageTitle>登录</PageTitle>
|
||||
|
||||
@if (!dataLoaded)
|
||||
@@ -67,7 +66,6 @@ else
|
||||
|
||||
private bool _isLoading = false;
|
||||
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
if (OperatingSystem.IsBrowser())
|
||||
@@ -85,12 +83,9 @@ else
|
||||
if (firstRender)
|
||||
{
|
||||
var authState = await AuthStateProvider.GetAuthenticationStateAsync();
|
||||
if (authState.User.Identity != null)
|
||||
if (authState.User.Identity != null && authState.User.Identity.IsAuthenticated)
|
||||
{
|
||||
if (authState.User.Identity.IsAuthenticated)
|
||||
{
|
||||
Navigation.NavigateTo(ReturnUrl ?? "/");
|
||||
}
|
||||
Navigation.NavigateTo(ReturnUrl ?? "/");
|
||||
}
|
||||
}
|
||||
if (!dataLoaded)
|
||||
@@ -102,32 +97,49 @@ else
|
||||
|
||||
private async Task LoginAsync()
|
||||
{
|
||||
if (form.Validate())
|
||||
if (!form.Validate()) return;
|
||||
|
||||
_isLoading = true;
|
||||
StateHasChanged();
|
||||
|
||||
try
|
||||
{
|
||||
// 请求后端登录接口,后端返回 ApiResult<AuthResponse>
|
||||
var api = "/api/sign/in";
|
||||
var result = await HttpService.Post<ApiResult<string>>(api, login);
|
||||
if (result.Success)
|
||||
var result = await HttpService.Post<ApiResult<AuthResponse>>(api, login);
|
||||
if (result.Success && result.Data != null)
|
||||
{
|
||||
Console.WriteLine("请求api成功");
|
||||
if (!string.IsNullOrEmpty(result.Data))
|
||||
var auth = result.Data;
|
||||
|
||||
// 保存 access + refresh 到 localStorage(WASM 场景)
|
||||
await localStorage.SetItemAsync("accessToken", auth.Token);
|
||||
await localStorage.SetItemAsync("refreshToken", auth.RefreshToken);
|
||||
|
||||
// 更新客户端 AuthenticationState(调用自定义 Provider 更新方法)
|
||||
if (AuthStateProvider is PersistentAuthenticationStateProvider provider)
|
||||
{
|
||||
await localStorage.SetItemAsStringAsync(StorageKeys.AccessToken, result.Data);
|
||||
await localStorage.SetItemAsStringAsync("refreshToken", result.Data);
|
||||
var authState = (AuthStateProvider as PersistentAuthenticationStateProvider);
|
||||
if (authState != null)
|
||||
{
|
||||
authState.UpdateAuthenticationState(result.Data);
|
||||
}
|
||||
Logger.LogInformation($"登录成功跳转目标,{ReturnUrl}");
|
||||
Navigation.NavigateTo(ReturnUrl ?? "/");
|
||||
// provider 仅需要 access token 更新来触发 UI 更新
|
||||
provider.UpdateAuthenticationState(auth.Token);
|
||||
}
|
||||
|
||||
Logger.LogInformation("登录成功,跳转: {ReturnUrl}", ReturnUrl);
|
||||
Navigation.NavigateTo(ReturnUrl ?? "/");
|
||||
}
|
||||
else
|
||||
{
|
||||
ModalService.Error(new ConfirmOptions() { Title = "提示", Content = result.Message });
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "登录失败");
|
||||
ModalService.Error(new ConfirmOptions() { Title = "错误", Content = "登录异常,请稍后重试" });
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isLoading = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OnPasswordKeyDown(KeyboardEventArgs value)
|
||||
|
||||
Reference in New Issue
Block a user