Rendre les actions de compte visibles sur l'accueil
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
@using System.Security.Claims
|
||||
@implements IAsyncDisposable
|
||||
@inject AuthenticationStateProvider AuthenticationStateProvider
|
||||
@inject NavigationManager Navigation
|
||||
@inject IJSRuntime JS
|
||||
|
||||
@@ -20,38 +21,26 @@
|
||||
<a class="@BuildNavLinkClass(RulesPaths)" href="reglement.html" aria-current="@BuildAriaCurrent(RulesPaths)">Reglement</a>
|
||||
</nav>
|
||||
|
||||
<AuthorizeView>
|
||||
<Authorized Context="authState">
|
||||
<div class="site-menu-account">
|
||||
<span class="micro-label">Compte Keycloak</span>
|
||||
<div class="site-menu-account-panel">
|
||||
<div class="site-menu-user">
|
||||
<strong>@BuildDisplayName(authState.User)</strong>
|
||||
<span>@BuildMeta(authState.User)</span>
|
||||
</div>
|
||||
<a class="button ghost small" href="@LogoutHref">Se deconnecter</a>
|
||||
<div class="site-menu-account">
|
||||
<span class="micro-label">Compte Keycloak</span>
|
||||
@if (IsAuthenticated)
|
||||
{
|
||||
<div class="site-menu-account-panel">
|
||||
<div class="site-menu-user">
|
||||
<strong>@DisplayName</strong>
|
||||
<span>@DisplayMeta</span>
|
||||
</div>
|
||||
<a class="button ghost small" href="@LogoutHref">Se deconnecter</a>
|
||||
</div>
|
||||
</Authorized>
|
||||
<Authorizing>
|
||||
<div class="site-menu-account">
|
||||
<span class="micro-label">Compte Keycloak</span>
|
||||
<div class="site-menu-account-actions">
|
||||
<button class="button secondary small" type="button" @onclick="OpenLoginModal">Se connecter</button>
|
||||
<button class="button ghost small" type="button" @onclick="OpenRegisterModal">Creer un compte</button>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="site-menu-account-actions">
|
||||
<button class="button secondary small" type="button" @onclick="OpenLoginModal">Se connecter</button>
|
||||
<button class="button ghost small" type="button" @onclick="OpenRegisterModal">Creer un compte</button>
|
||||
</div>
|
||||
</Authorizing>
|
||||
<NotAuthorized>
|
||||
<div class="site-menu-account">
|
||||
<span class="micro-label">Compte Keycloak</span>
|
||||
<div class="site-menu-account-actions">
|
||||
<button class="button secondary small" type="button" @onclick="OpenLoginModal">Se connecter</button>
|
||||
<button class="button ghost small" type="button" @onclick="OpenRegisterModal">Creer un compte</button>
|
||||
</div>
|
||||
</div>
|
||||
</NotAuthorized>
|
||||
</AuthorizeView>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
</div>
|
||||
@@ -85,9 +74,13 @@
|
||||
|
||||
private DotNetObjectReference<SiteMenu>? _dotNetReference;
|
||||
private bool _listenerRegistered;
|
||||
private bool _authStateSubscribed;
|
||||
private string? AuthModalSource;
|
||||
private string AuthModalTitle = "Authentification";
|
||||
private bool ShowAuthModal;
|
||||
private bool IsAuthenticated;
|
||||
private string DisplayName = "Utilisateur connecte";
|
||||
private string DisplayMeta = "Session active";
|
||||
|
||||
private string LoginHref => BuildAuthHref("login", EffectiveReturnUrl);
|
||||
private string RegisterHref => BuildAuthHref("register", EffectiveReturnUrl);
|
||||
@@ -140,6 +133,13 @@
|
||||
=> user.FindFirst("email")?.Value
|
||||
?? "Session active";
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
AuthenticationStateProvider.AuthenticationStateChanged += HandleAuthenticationStateChanged;
|
||||
_authStateSubscribed = true;
|
||||
await RefreshAuthenticationStateAsync();
|
||||
}
|
||||
|
||||
private async Task OpenLoginModal()
|
||||
=> await OpenAuthModalAsync("Se connecter", LoginHref);
|
||||
|
||||
@@ -196,8 +196,47 @@
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void HandleAuthenticationStateChanged(Task<AuthenticationState> authenticationStateTask)
|
||||
=> _ = InvokeAsync(RefreshAuthenticationStateAsync);
|
||||
|
||||
private async Task RefreshAuthenticationStateAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
|
||||
var user = authState.User;
|
||||
|
||||
if (user.Identity?.IsAuthenticated == true)
|
||||
{
|
||||
IsAuthenticated = true;
|
||||
DisplayName = BuildDisplayName(user);
|
||||
DisplayMeta = BuildMeta(user);
|
||||
}
|
||||
else
|
||||
{
|
||||
ResetAuthenticationDisplay();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
ResetAuthenticationDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetAuthenticationDisplay()
|
||||
{
|
||||
IsAuthenticated = false;
|
||||
DisplayName = "Utilisateur connecte";
|
||||
DisplayMeta = "Session active";
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_authStateSubscribed)
|
||||
{
|
||||
AuthenticationStateProvider.AuthenticationStateChanged -= HandleAuthenticationStateChanged;
|
||||
}
|
||||
|
||||
if (_listenerRegistered)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user