The correct and best way to redirect an user after the login in Drupal 9
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Implements hook_form_FORM_ID_alter().
*/
function MYCUSTOMMODULE_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['#submit'][] = 'MYCUSTOMMODULE_user_login_form_submit';
}
/**
* Custom submit handler for the login form.
*/
function MYCUSTOMMODULE_user_login_form_submit($form, FormStateInterface $form_state) {
// Do some stuff here, if you want.
// If the destination path is not a regular Drupal path, e.g. a view, use
// Url::fromUri('base:/your/destination/path'); instead of Url::fromRoute.
$url = Url::fromRoute('/your/destination/path');
$form_state->setRedirectUrl($url);
}