LANCommander/LANCommander.Client/UI/Authenticate/Index.razor

106 lines
2.9 KiB
Text

@page "/Authenticate"
@using BeaconLib
@inject ProfileService ProfileService
@inject NavigationManager NavigationManager
<Form Model="@Model" Loading="@Loading" OnFinish="OnFinish">
<FormItem>
<Input @bind-Value="@context.ServerAddress" />
<Card Title="Discovered Servers">
<Extra>
@if (BeaconActive)
{
<Icon Type="loading" Theme="outline" />
}
</Extra>
<Body>
<AntList Bordered DataSource="DiscoveredServers" Context="server" Size="small" Class="ant-list-clickable">
<ListItem OnClick="() => Model.ServerAddress = server">
<Text>@server</Text>
</ListItem>
</AntList>
</Body>
</Card>
</FormItem>
<FormItem>
<Input @bind-Value="@context.Username" />
</FormItem>
<FormItem>
<InputPassword @bind-Value="@context.Password" />
</FormItem>
<FormItem>
<Button Type="@ButtonType.Primary" HtmlType="submit">
Login
</Button>
</FormItem>
</Form>
@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)
{
try
{
await ProfileService.Login(Model.ServerAddress, Model.Username, Model.Password);
NavigationManager.NavigateTo("/", true);
}
catch (Exception ex)
{
}
}
}