feat(auth): Implementazione completa sistema autenticazione
BREAKING CHANGE: Tutte le pagine ora richiedono autenticazione Nuove funzionalità: - Sistema di login con password hardcoded (admin123) - Form di login full-screen con gradiente viola - Protezione automatica di tutte le route - Pulsante logout visibile in tutte le pagine - Gestione thread-safe eventi autenticazione con InvokeAsync() Componenti: - AuthenticationService: servizio Singleton per gestione stato - Login.razor: pagina login con validazione e messaggi errore - App.razor: routing condizionale basato su autenticazione - MainLayout.razor: pulsante logout integrato Fix tecnici: - Risolto errore "Dispatcher not associated" usando InvokeAsync() - Implementato pattern corretto per eventi cross-thread in Blazor Server - Aggiunto Dispose per prevenire memory leak
This commit is contained in:
+56
-12
@@ -1,12 +1,56 @@
|
||||
<Router AppAssembly="@typeof(App).Assembly">
|
||||
<Found Context="routeData">
|
||||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
|
||||
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
|
||||
</Found>
|
||||
<NotFound>
|
||||
<PageTitle>Not found</PageTitle>
|
||||
<LayoutView Layout="@typeof(MainLayout)">
|
||||
<p role="alert">Sorry, there's nothing at this address.</p>
|
||||
</LayoutView>
|
||||
</NotFound>
|
||||
</Router>
|
||||
@using Data_Coupler.Services
|
||||
@using Data_Coupler.Pages
|
||||
@inject IAuthenticationService AuthService
|
||||
@implements IDisposable
|
||||
|
||||
@if (!AuthService.IsAuthenticated)
|
||||
{
|
||||
<Login />
|
||||
}
|
||||
else
|
||||
{
|
||||
<Router AppAssembly="@typeof(App).Assembly">
|
||||
<Found Context="routeData">
|
||||
@{
|
||||
// Se l'utente prova ad accedere alla pagina di login mentre è autenticato, reindirizza alla home
|
||||
if (routeData.PageType == typeof(Data_Coupler.Pages.Login))
|
||||
{
|
||||
<RouteView RouteData="@CreateHomeRouteData()" DefaultLayout="@typeof(MainLayout)" />
|
||||
}
|
||||
else
|
||||
{
|
||||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
|
||||
}
|
||||
}
|
||||
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
|
||||
</Found>
|
||||
<NotFound>
|
||||
<PageTitle>Not found</PageTitle>
|
||||
<LayoutView Layout="@typeof(MainLayout)">
|
||||
<p role="alert">Sorry, there's nothing at this address.</p>
|
||||
</LayoutView>
|
||||
</NotFound>
|
||||
</Router>
|
||||
}
|
||||
|
||||
@code {
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
AuthService.OnAuthenticationStateChanged += OnAuthenticationStateChanged;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
AuthService.OnAuthenticationStateChanged -= OnAuthenticationStateChanged;
|
||||
}
|
||||
|
||||
private void OnAuthenticationStateChanged()
|
||||
{
|
||||
InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
private RouteData CreateHomeRouteData()
|
||||
{
|
||||
return new RouteData(typeof(Data_Coupler.Pages.DataCoupler), new Dictionary<string, object?>());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user