public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
return new RedirectResponse($targetPath);
}
// Get the authenticated user
$user = $token->getUser();
// Check the user's role
if ($user->getRoles() === 'ROLE_ADMIN') {
// Redirect to the app_admin page
return new RedirectResponse($this->urlGenerator->generate('app_admin'));
} elseif ($user->getRoles() === 'ROLE_INSTRUCTEUR') {
// Redirect to the app_instructeur page
return new RedirectResponse($this->urlGenerator->generate('app_instructeur'));
} else {
// Redirect to the app_user page
return new RedirectResponse($this->urlGenerator->generate('app_user'));
}
}
I'm working on a Symfony 5.4 application, and I have a custom authentication success handler where I'm checking the user's roles to determine the appropriate redirection. However, I seem to be encountering an issue with the role checking logic.