Redirect user after login

By nnevill, 25 April, 2023

It's quite an often case when we need to redirect the user somewhere after login. The redirect path could depend on the user role, the user's field values, etc. 

The easiest way to do so is to implement hook_user_login:

/**
 * Implements hook_user_login().
 */
function custom_module_user_login(User $account) {
  // Let's redirect user to /dashboard page.
  \Drupal::service('request_stack')->getCurrentRequest()->query->set('destination', '/dashboard');
}