LANCommander/LANCommander.Client/UI/Authenticate/Index.razor
2024-06-08 14:32:39 -05:00

104 lines
3 KiB
Text

@page "/Authenticate"
@using BeaconLib
@inject ProfileService ProfileService
@inject NavigationManager NavigationManager
@inject IMessageService MessageService
<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" />
</div>
<Form Model="@Model" Loading="@Loading" Layout="@FormLayout.Vertical" OnFinish="OnFinish">
<FormItem Label="Server Address">
<AutoComplete Options="DiscoveredServers">
<AutoCompleteInput @bind-Value="@context.ServerAddress" />
</AutoComplete>
</FormItem>
<FormItem Label="Username">
<Input @bind-Value="@context.Username" />
</FormItem>
<FormItem Label="Password">
<InputPassword @bind-Value="@context.Password" />
</FormItem>
<FormItem>
<Button Type="@ButtonType.Primary" HtmlType="submit">
Login
</Button>
</FormItem>
</Form>
</Content>
</Layout>
@code {
public class AuthenticationRequest
{
public string ServerAddress { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
AuthenticationRequest Model = new AuthenticationRequest();
bool Loading { get; set; } = false;
List<string> DiscoveredServers = new List<string>();
bool BeaconActive = true;
Probe Probe = new Probe("LANCommander");
protected override async Task OnInitializedAsync()
{
Probe.BeaconsUpdated += (beacons) =>
{
foreach (var beacon in beacons)
{
if (!String.IsNullOrWhiteSpace(beacon.Data) && Uri.TryCreate(beacon.Data, UriKind.Absolute, out var beaconUri))
{
if (!DiscoveredServers.Contains(beaconUri.ToString()))
DiscoveredServers.Add(beaconUri.ToString());
else
{
var address = $"http://{beacon.Address.Address}:{beacon.Address.Port}";
if (!DiscoveredServers.Contains(address))
DiscoveredServers.Add(address);
}
}
}
};
ActivateBeacon();
}
async Task ActivateBeacon()
{
BeaconActive = true;
Probe.Start();
await Task.Delay(10000);
Probe.Stop();
BeaconActive = false;
await InvokeAsync(StateHasChanged);
}
async Task OnFinish(EditContext editContext)
{
Loading = true;
try
{
await ProfileService.Login(Model.ServerAddress, Model.Username, Model.Password);
NavigationManager.NavigateTo("/");
}
catch (Exception ex)
{
MessageService.Error(ex.Message);
Loading = false;
}
}
}