Add authentication support with login/logout UI

- Introduced AuthService, IAuthApiClient, and AuthApiClient for managing authentication state and API calls (login, logout, session restore).
- Added Login.razor and LoginLayout.razor for the login page, including styling and logic.
- MainLayout.razor now checks authentication on load, restores sessions from sessionStorage, and redirects to /login if unauthenticated. Displays username and logout button when logged in.
- Implemented JS interop (authStorage) for persisting authentication info in sessionStorage.
- Registered AuthService, CookieContainer, and API clients in Program.cs to share cookies and support authentication.
- Updated AppSettings and appsettings files to support separate ApiBaseUrl and DataApiBaseUrl.
- Minor CSS improvements for username display in the top bar.
This commit is contained in:
OlgunR
2026-05-12 16:32:46 +02:00
parent 45011122b2
commit 1ad267e409
13 changed files with 462 additions and 11 deletions

View File

@@ -0,0 +1,106 @@
@page "/login"
@layout DbFirst.BlazorWebApp.Components.Layout.LoginLayout
@rendermode InteractiveServer
@inject IAuthApiClient AuthApiClient
@inject AuthService AuthService
@inject NavigationManager Navigation
@inject IJSRuntime JS
<PageTitle>Anmelden DbFirst</PageTitle>
<div class="login-container">
<div class="login-card">
<div class="login-header">
<span class="login-brand">DbFirst</span>
<p class="login-subtitle">Bitte melden Sie sich an</p>
</div>
<form @onsubmit="HandleLoginAsync" @onsubmit:preventDefault>
<div class="login-field">
<label class="login-label" for="username">Benutzername</label>
<input id="username"
type="text"
class="login-text-input"
placeholder="Benutzername eingeben"
autocomplete="username"
@bind="_username"
@bind:event="oninput" />
</div>
<div class="login-field">
<label class="login-label" for="password">Passwort</label>
<input id="password"
type="password"
class="login-text-input"
placeholder="Passwort eingeben"
autocomplete="current-password"
@bind="_password"
@bind:event="oninput" />
</div>
@if (!string.IsNullOrEmpty(_errorMessage))
{
<div class="alert alert-danger mt-2 mb-1" role="alert">
@_errorMessage
</div>
}
<div class="login-actions">
<button type="submit"
class="login-submit-btn"
disabled="@_isLoading">
@(_isLoading ? "Wird angemeldet…" : "Anmelden")
</button>
</div>
</form>
</div>
</div>
@code {
private string _username = string.Empty;
private string _password = string.Empty;
private string _errorMessage = string.Empty;
private bool _isLoading;
protected override void OnInitialized()
{
if (AuthService.IsAuthenticated)
Navigation.NavigateTo("/");
}
private async Task HandleLoginAsync()
{
if (_isLoading) return;
_errorMessage = string.Empty;
if (string.IsNullOrWhiteSpace(_username) || string.IsNullOrWhiteSpace(_password))
{
_errorMessage = "Bitte Benutzername und Passwort eingeben.";
return;
}
_isLoading = true;
try
{
var success = await AuthApiClient.LoginAsync(_username, _password);
if (success)
{
await JS.InvokeVoidAsync("authStorage.set", AuthService.UserName, AuthService.RawCookieHeader);
Navigation.NavigateTo("/");
}
else
{
_errorMessage = "Anmeldung fehlgeschlagen. Bitte Benutzerdaten prüfen.";
}
}
catch
{
_errorMessage = "Verbindungsfehler. Bitte später erneut versuchen.";
}
finally
{
_isLoading = false;
}
}
}