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:
106
DbFirst.BlazorWebApp/Components/Pages/Login.razor
Normal file
106
DbFirst.BlazorWebApp/Components/Pages/Login.razor
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
111
DbFirst.BlazorWebApp/Components/Pages/Login.razor.css
Normal file
111
DbFirst.BlazorWebApp/Components/Pages/Login.razor.css
Normal file
@@ -0,0 +1,111 @@
|
||||
.login-container {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-height: calc(100vh - 2rem);
|
||||
}
|
||||
|
||||
.login-card {
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
padding: 2rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid var(--band-editor-border, #dee2e6);
|
||||
background-color: var(--band-editor-bg, #fff);
|
||||
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
|
||||
.login-header {
|
||||
text-align: center;
|
||||
margin-bottom: 1.75rem;
|
||||
}
|
||||
|
||||
.login-brand {
|
||||
font-size: 1.8rem;
|
||||
font-weight: 700;
|
||||
color: #1b6ec2;
|
||||
letter-spacing: 0.02em;
|
||||
}
|
||||
|
||||
.login-subtitle {
|
||||
margin-top: 0.35rem;
|
||||
margin-bottom: 0;
|
||||
font-size: 0.9rem;
|
||||
color: #6c757d;
|
||||
}
|
||||
|
||||
.login-field {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.login-label {
|
||||
display: block;
|
||||
font-size: 0.85rem;
|
||||
font-weight: 500;
|
||||
margin-bottom: 0.3rem;
|
||||
}
|
||||
|
||||
.login-actions {
|
||||
margin-top: 1.25rem;
|
||||
}
|
||||
|
||||
/* Gemeinsamer Stil für Text- und Passwort-Eingabefelder */
|
||||
.login-text-input {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 2.25rem;
|
||||
padding: 0.25rem 0.65rem;
|
||||
font-size: 1rem;
|
||||
font-family: inherit;
|
||||
line-height: 1.5;
|
||||
color: inherit;
|
||||
background-color: transparent;
|
||||
border: 1px solid #ced4da;
|
||||
border-radius: 4px;
|
||||
box-sizing: border-box;
|
||||
outline: none;
|
||||
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
|
||||
}
|
||||
|
||||
.login-text-input:focus {
|
||||
border-color: #86b7fe;
|
||||
box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.25);
|
||||
}
|
||||
|
||||
/* Submit-Button */
|
||||
.login-submit-btn {
|
||||
display: block;
|
||||
width: 100%;
|
||||
padding: 0.45rem 1rem;
|
||||
font-size: 1rem;
|
||||
font-family: inherit;
|
||||
font-weight: 500;
|
||||
color: #fff;
|
||||
background-color: #1b6ec2;
|
||||
border: 1px solid #1861ac;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out;
|
||||
}
|
||||
|
||||
.login-submit-btn:hover:not(:disabled) {
|
||||
background-color: #1558a0;
|
||||
border-color: #1456a0;
|
||||
}
|
||||
|
||||
.login-submit-btn:disabled {
|
||||
opacity: 0.65;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* Dark-Mode */
|
||||
.app-dark .login-text-input {
|
||||
border-color: #555;
|
||||
color: #e8e8e8;
|
||||
background-color: #2d2d2d;
|
||||
}
|
||||
|
||||
.app-dark .login-text-input:focus {
|
||||
border-color: #6cb6ff;
|
||||
box-shadow: 0 0 0 0.2rem rgba(108, 182, 255, 0.25);
|
||||
}
|
||||
Reference in New Issue
Block a user