Redirect the user logged in based on user role in drupal 9
You can use hook_user_login to redirect the user logged in based on user role in drupal 9:
/**
* Implements hook_user_login().
*/
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;
function modulename_user_login($account) {
$routeName = 'entity.node.canonical';
$nodeId = 1;
$routeParameters = ['node' => $nodeId];
$url = \Drupal::url($routeName, $routeParameters);
$roles = $account->getRoles();
if(in_array('administrator', $roles)){
$response = new RedirectResponse($url);
$response->send();
return;
}
}