LANCommander/LANCommander.Launcher/UI/Pages/Authenticate/Components/RegistrationForm.razor
Pat Hartl acb91f8484 Fix login/logout UX
- Moves authentication back to a /Authenticate page route
- Flatten the namespaces of page components
- Implement top-level layouts as windows
- Move handling of redirecting to /Authenticate with component inside of an AuthenticatedLayout
- Use individiual injected services instead of SDK.Client
- Wipe out tokens on logout
- Process logout even if server can't be reached
2025-11-07 20:19:13 -06:00

98 lines
No EOL
2.9 KiB
Text

@using LANCommander.SDK.Exceptions
@using LANCommander.SDK.Models
@namespace LANCommander.Launcher.UI.Components
@inject AuthenticationService AuthenticationService
@inject SDK.Client Client
@inject NavigationManager NavigationManager
@inject IMessageService MessageService
@inject ILogger<RegistrationForm> Logger
@inject ImportManagerService ImportManagerService
@inject LocalizationService LocalizationService
<PageHeader OnBack="OnBack">
<TitleTemplate>
@LocalizationService.GetString("Register")
</TitleTemplate>
</PageHeader>
<Form Model="@Model" Loading="@Loading" Layout="FormLayout.Vertical" OnFinish="OnFinish">
@foreach (var item in Errors)
{
<Alert Message="@item" Type="AlertType.Error" ShowIcon="true" Style="margin-bottom: 8px;" />
}
<FormItem Label="@LocalizationService.GetString("ServerAddress")">
<Input Value="ServerAddress" Disabled />
</FormItem>
<FormItem Label="@LocalizationService.GetString("Username")">
<Input @bind-Value="@context.UserName" AutoFocus />
</FormItem>
<FormItem Label="@LocalizationService.GetString("Password")">
<InputPassword @bind-Value="@context.Password"/>
</FormItem>
<FormItem Label="@LocalizationService.GetString("ConfirmPassword")">
<InputPassword @bind-Value="@context.PasswordConfirmation" />
</FormItem>
<FormItem>
<Button Type="ButtonType.Primary" HtmlType="submit">
@LocalizationService.GetString("Register")
</Button>
</FormItem>
</Form>
@code {
[Parameter] public Uri ServerAddress { get; set; }
[Parameter] public EventCallback OnBack { get; set; }
[Parameter] public IEnumerable<string> Errors { get; set; } = [];
RegistrationRequest Model = new();
bool Loading = false;
private Uri? PreviousServerAddress;
protected override async Task OnParametersSetAsync()
{
if (ServerAddress != PreviousServerAddress)
await Client.Connection.UpdateServerAddressAsync(ServerAddress.ToString());
PreviousServerAddress = ServerAddress;
ClearErrors();
}
protected void ClearErrors()
{
Errors = [];
StateHasChanged();
}
async Task OnFinish(EditContext editContext)
{
Loading = true;
try
{
ClearErrors();
await AuthenticationService.Register(Model.UserName, Model.Password, Model.PasswordConfirmation);
await ImportManagerService.RequestImport();
NavigationManager.NavigateTo("/");
}
catch (RegisterFailedException ex)
{
Errors = ex.ErrorData.Details?.Select(x => x.Message) ?? [];
MessageService.Error(ex.Message, 5);
}
catch (Exception ex)
{
MessageService.Error(ex.Message, 5);
Logger.LogError(ex, ex.Message);
}
finally
{
Loading = false;
}
}
}