LANCommander/LANCommander.Launcher/UI/Authenticate/Index.razor
Pat Hartl 9ba9887eb1 Rename project files, namespaces, artifacts
Client has been renamed to launcher, and thus namespaces and artifact names had to change. The server project has also had some artifacts renamed in order to create a more clear separation of builds. This will break auto-updates before v0.9.0.
2024-08-04 17:53:19 -05:00

124 lines
3.6 KiB
Text

@page "/Authenticate"
@using BeaconLib
@inject ProfileService ProfileService
@inject NavigationManager NavigationManager
@inject IMessageService MessageService
@inject ILogger<Index> Logger
<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>
@if (Settings.Profile.Id != Guid.Empty && !String.IsNullOrWhiteSpace(Settings.Profile.Alias))
{
<Button OnClick="OfflineMode">
Offline
</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>();
Models.Settings Settings = SettingService.GetSettings();
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);
MainLayout.Import();
NavigationManager.NavigateTo("/");
}
catch (Exception ex)
{
MessageService.Error(ex.Message, 5);
Logger.LogError(ex, ex.Message);
Loading = false;
}
}
async Task OfflineMode()
{
Settings.Authentication.OfflineMode = true;
SettingService.SaveSettings(Settings);
NavigationManager.NavigateTo("/");
}
}