LANCommander/LANCommander.Launcher/UI/Authenticate/Components/LoginForm.razor

189 lines
No EOL
6 KiB
Text

@using LANCommander.SDK.Exceptions
@using LANCommander.SDK.Models
@inject AuthenticationService AuthenticationService
@inject SDK.Client Client
@inject NavigationManager NavigationManager
@inject IMessageService MessageService
@inject ILogger<LoginForm> Logger
@inject ImportManagerService ImportManagerService
@inject LocalizationService LocalizationService
<PageHeader OnBack="OnBack">
<TitleTemplate>
@LocalizationService.GetString("SignIn")
</TitleTemplate>
</PageHeader>
<Form Model="@Model" Loading="@Loading" Layout="FormLayout.Vertical" OnFinish="OnFinish">
@foreach (var item in Errors)
{
<Alert Message="@item" Type="AlertType.Error" ShowIcon="true" Style="margin-bottom: 8px;" />
}
<FormItem Label="@LocalizationService.GetString("ServerAddress")">
<Input Value="ServerAddress" Disabled />
</FormItem>
<FormItem Label="@LocalizationService.GetString("Username")">
<Input @bind-Value="@context.UserName" AutoFocus />
</FormItem>
<FormItem Label="@LocalizationService.GetString("Password")">
<InputPassword @bind-Value="@context.Password"/>
</FormItem>
<FormItem>
<Button Type="ButtonType.Primary" HtmlType="submit">
@LocalizationService.GetString("Login")
</Button>
<Button Type="ButtonType.Text" OnClick="OnRegister">
@LocalizationService.GetString("Register")
</Button>
</FormItem>
</Form>
@if (AuthenticationProviders.Any())
{
<Divider Orientation="DividerOrientation.Center" Text="@LocalizationService.GetString("Or")" Style="margin-bottom: 16px" />
<div class="authentication-provider-container">
<div class="authentication-provider-button-group">
@foreach (var authenticationProvider in AuthenticationProviders)
{
<div class="authentication-provider-button">
<Button
Type="ButtonType.Primary"
OnClick="() => AuthenticationProviderDialog.Open(ServerAddress, authenticationProvider)"
Class="authentication-provider-btn"
Style="@(!String.IsNullOrWhiteSpace(authenticationProvider.Color) ? $"background: {authenticationProvider.Color}; border-color: {authenticationProvider.Color}" : "")">
@if (!String.IsNullOrWhiteSpace(authenticationProvider.Icon))
{
<BootstrapIcon Type="@authenticationProvider.Icon"/>
}
@LocalizationService.GetString("SignInUsingProvider", authenticationProvider.Name)
</Button>
</div>
}
</div>
</div>
}
<AuthenticationProviderDialog @ref="AuthenticationProviderDialog" OnTokenReceived="UseToken" />
@code {
[Parameter] public Uri ServerAddress { get; set; }
[Parameter] public EventCallback OnBack { get; set; }
[Parameter] public EventCallback OnRegister { get; set; }
[Parameter] public IEnumerable<string> Errors { get; set; } = [];
List<AuthenticationProvider> AuthenticationProviders = new();
AuthRequest Model = new();
bool Loading = false;
private Uri? PreviousServerAddress;
AuthenticationProviderDialog AuthenticationProviderDialog;
Models.Settings Settings = SettingService.GetSettings();
protected override async Task OnInitializedAsync()
{
AuthenticationProviders = new();
await Client.Connection.UpdateServerAddressAsync(ServerAddress);
ClearErrors();
try
{
var authenticationProviders = await Client.Authentication.GetAuthenticationProvidersAsync();
if (authenticationProviders != null && authenticationProviders.Any())
AuthenticationProviders = authenticationProviders.ToList();
}
catch (Exception ex)
{
Logger.LogError(ex, "Could not get authentication providers");
}
StateHasChanged();
}
protected void ClearErrors()
{
Errors = [];
StateHasChanged();
}
protected override async Task OnParametersSetAsync()
{
if (ServerAddress != PreviousServerAddress)
await Client.Connection.UpdateServerAddressAsync(ServerAddress);
PreviousServerAddress = ServerAddress;
}
async Task OnFinish(EditContext editContext)
{
Loading = true;
try
{
ClearErrors();
await AuthenticationService.Login(Client.Connection.GetServerAddress(), Model.UserName, Model.Password);
await ImportManagerService.RequestImport();
NavigationManager.NavigateTo("/");
MessageService.Success(LocalizationService.GetString("WelcomeBack"));
}
catch (AuthFailedException ex)
{
Errors = ex.ErrorData.Details?.Select(x => x.Message) ?? [];
MessageService.Error(ex.Message, 5);
}
catch (Exception ex)
{
MessageService.Error(ex.Message, 5);
Logger.LogError(ex, ex.Message);
}
finally
{
Loading = false;
}
}
void Back()
{
AuthenticationProviders = new();
NavigationManager.NavigateTo("/Authenticate");
}
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;
}
}
async Task OfflineMode()
{
Settings.Authentication.OfflineMode = true;
SettingService.SaveSettings(Settings);
NavigationManager.NavigateTo("/");
}
}