Home > Software engineering >  Blazor - Prevent going to login route when user is authenticated
Blazor - Prevent going to login route when user is authenticated

Time:08-02

I have a client Blazor WASM project, which is authenticated with Bearer Token that receives from an API (other project). I have managed to implement the authentication process using AuthenticationStateProvider, and manage the UI components to show/hide depending on the authentication state.

One thing I cannot achieve is to prevent user from manually routing to /login if the user is Authenticated. How can I achieve this ?

CodePudding user response:

You can redirect user when already logged in:

[Inject] NavigationManager Navigation;

[CascadingParameter] public Task<AuthenticationState> authStateTask {get; set;}

protected override async Task OnInitializedAsync()
{
  var user = (await authStateTask).User;
  if (user.Identity.IsAuthenticated)
    Navigation.NavigateTo("/");    
}
  • Related