124 lines
3.6 KiB
Text
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("/");
|
|
}
|
|
}
|