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