LANCommander/LANCommander.Launcher/UI/Components/AuthenticationForm.razor

94 lines
No EOL
3 KiB
Text

@using System.Security.Policy
@using LANCommander.Launcher.Models
@using LANCommander.SDK
@using LANCommander.SDK.Models
@using LANCommander.Launcher.UI.Authenticate.Components
@inject ProfileService ProfileService
@inject NavigationManager NavigationManager
@inject SDK.Client Client
@inject AuthenticationService AuthenticationService
@inject IMessageService MessageService
@inject ILogger<System.Index> Logger
<CascadingValue Value="State">
<Layout Style="background-image: url('/assets/auth-background.jpg'); background-size: cover;">
<Content Class="authentication-form">
<div class="authentication-logo">
<img src="/assets/logo.svg" width="300" />
</div>
<div class="authentication-box">
@if (State.Stage == AuthenticationStage.SelectServer)
{
<ServerSelector OnSelected="ServerSelected" Connecting="_connecting" />
}
@if (State.Stage == AuthenticationStage.Login)
{
<LoginForm ServerAddress="@State.ServerAddress" OnBack="() => State.Stage = AuthenticationStage.SelectServer" OnRegister="() => State.Stage = AuthenticationStage.Register" />
}
@if (State.Stage == AuthenticationStage.Register)
{
<RegistrationForm ServerAddress="@State.ServerAddress" OnBack="() => State.Stage = AuthenticationStage.Login" />
}
</div>
</Content>
</Layout>
</CascadingValue>
@code {
[CascadingParameter] public Models.ConnectionState ConnectionState { get; set; }
AuthenticationFormState State { get; set; }
bool _connecting { get; set; } = false;
private Models.Settings Settings = SettingService.GetSettings();
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
State = new()
{
Stage = AuthenticationStage.None,
ServerAddress = AuthenticationService.GetServerAddress(),
};
bool hasServer = await AuthenticationService.IsServerOnlineAsync();
if (hasServer)
{
if (!ConnectionState.IsStartup)
{
State.Stage = AuthenticationService.HasStoredCredentials() || Settings.LaunchCount > 0
? AuthenticationStage.Login
: AuthenticationStage.Register;
}
}
else
{
State.Stage = AuthenticationStage.SelectServer;
}
}
async Task ServerSelected(string serverAddress)
{
try
{
_connecting = true;
await Client.ChangeServerAddressAsync(serverAddress);
State.ServerAddress = Client.GetServerAddress();
State.Stage = AuthenticationStage.Login;
}
catch (Exception ex)
{
MessageService.Error(ex.Message);
}
finally
{
_connecting = false;
}
}
}