C
C#15mo ago
Hercules

❔ Blazor WebAssembly: How to dynamically hide a layout component after logging out

Question: I have a Blazor WebAssembly application where I'm using the MudBlazor library to create a layout that includes a navigation bar (MudAppBar). I want to dynamically hide this navigation bar after a user logs out of the application. Here's the relevant part of my layout:
@if (AuthState != null && AuthState.User.Identity.IsAuthenticated)
{
<!-- MudAppBar and other layout components -->
}
else
{
<!-- Layout without MudAppBar -->
}
@if (AuthState != null && AuthState.User.Identity.IsAuthenticated)
{
<!-- MudAppBar and other layout components -->
}
else
{
<!-- Layout without MudAppBar -->
}
I'm triggering the logout process using the following code:
private async Task<bool> SignOut()
{
var isLoggedOut = await _JwtAuthProvider.Logout();
if (isLoggedOut)
{
_navigationManager.NavigateTo("/login");
}
return isLoggedOut;
}
private async Task<bool> SignOut()
{
var isLoggedOut = await _JwtAuthProvider.Logout();
if (isLoggedOut)
{
_navigationManager.NavigateTo("/login");
}
return isLoggedOut;
}
Inside my Logout method, I'm using NotifyAuthenticationStateChanged to reset the authentication state and set it to an empty ClaimsPrincipal. However, after navigating to the login page (/login), the navigation bar (MudAppBar) is still visible. It seems that the main layout is rendered when _navigationManager.NavigateTo("/login") is invoked. How can I ensure that the navigation bar is hidden after a user logs out and navigates to the login page? Is there a way to dynamically update the layout based on the authentication state without requiring a hard refresh of the page? I appreciate any insights or suggestions to resolve this issue. Here is my custom AuthenticationStateProvider - how i user NotifyAuthenticationStateChanged - -> check the Logout() method. https://paste.mod.gg/kvvtgasbzsxo/0
BlazeBin - kvvtgasbzsxo
A tool for sharing your source code with the world!
1 Reply
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server