LANCommander/LANCommander.Launcher/UI/Authenticate/Components/RegistrationForm.razor
2025-02-06 20:54:03 -06:00

90 lines
No EOL
2.5 KiB
Text

@using LANCommander.SDK.Models
@inject AuthenticationService AuthenticationService
@inject SDK.Client Client
@inject NavigationManager NavigationManager
@inject IMessageService MessageService
@inject ILogger<RegistrationForm> Logger
@inject ImportManagerService ImportManagerService
<PageHeader OnBack="OnBack">
<TitleTemplate>
Register
</TitleTemplate>
</PageHeader>
<Form Model="@Model" Loading="@Loading" Layout="FormLayout.Vertical" OnFinish="OnFinish">
<FormItem Label="Server Address">
<Input Value="ServerAddress" Disabled />
</FormItem>
<FormItem Label="Username">
<Input @bind-Value="@context.UserName"/>
</FormItem>
<FormItem Label="Password">
<InputPassword @bind-Value="@context.Password"/>
</FormItem>
<FormItem Label="Confirm Password">
<InputPassword @bind-Value="@context.PasswordConfirmation" />
</FormItem>
<FormItem>
<Button Type="ButtonType.Primary" HtmlType="submit">
Register
</Button>
</FormItem>
</Form>
@code {
[Parameter] public string ServerAddress { get; set; }
[Parameter] public EventCallback OnBack { get; set; }
RegistrationRequest Model = new();
bool Loading = false;
Models.Settings Settings = SettingService.GetSettings();
protected override async Task OnParametersSetAsync()
{
Client.ChangeServerAddress(ServerAddress);
StateHasChanged();
}
async Task OnFinish(EditContext editContext)
{
Loading = true;
try
{
await AuthenticationService.Register(Client.GetServerAddress(), Model.UserName, Model.Password, Model.PasswordConfirmation);
await ImportManagerService.RequestImport();
NavigationManager.NavigateTo("/");
}
catch (Exception ex)
{
MessageService.Error(ex.Message, 5);
Logger.LogError(ex, ex.Message);
Loading = false;
}
}
async Task UseToken(AuthToken token)
{
Settings.Authentication.AccessToken = token.AccessToken;
Settings.Authentication.RefreshToken = token.RefreshToken;
try
{
await AuthenticationService.Login(ServerAddress, token);
await ImportManagerService.RequestImport();
NavigationManager.NavigateTo("/");
}
catch (Exception ex)
{
MessageService.Error(ex.Message, 5);
Logger.LogError(ex, ex.Message);
Loading = false;
}
}
}