Files
FACULTATE-HEALTHCARE_MANAGER/frontend/UI/RoleBasedRedirect.cs
T
2024-05-30 15:40:13 +03:00

27 lines
1.1 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
namespace HealthcareManagerUiWebAssem;
public class RoleBasedRedirect : ComponentBase
{
[CascadingParameter] private Task<AuthenticationState> authenticationStateTask { get; set; }
[Inject] private NavigationManager NavigationManager { get; set; }
protected override async Task OnInitializedAsync()
{
var authState = await authenticationStateTask;
var user = authState.User;
if (NavigationManager.Uri == NavigationManager.BaseUri)
if (user.Identity.IsAuthenticated)
{
// Perform redirection based on the user's role
if (user.IsInRole(UserRoles.Admin))
NavigationManager.NavigateTo("/admin/dashboard");
else if (user.IsInRole(UserRoles.Doctor))
NavigationManager.NavigateTo("/doctor/dashboard");
else if (user.IsInRole(UserRoles.Patient)) NavigationManager.NavigateTo("/patient/dashboard");
}
}
}