How to redirect users after login in drupal?

Drupal 8 and 9

Using custom code (source):

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_user_login_form_alter(&$form, FormStateInterface $form_state) {
  $form['#submit'][] = 'mymodule_user_login_submit';
}

/**
 * Form submission handler for user_login_form().
 *
 * Redirects the user to the dashboard after logging in.
 */
function mymodule_user_login_submit(&$form, FormStateInterface $form_state) {
  $url = Url::fromRoute('mymodule.dashboard');

  // Check if a destination was set, probably on an exception controller.
  // @see \Drupal\user\Form\UserLoginForm::submitForm()
  $request = \Drupal::service('request_stack')->getCurrentRequest();
  if (!$request->request->has('destination')) {
    $form_state->setRedirectUrl($url);
  }
  else {
    $request->query->set('destination', $request->request->get('destination'));
  }
}

Using a contributed module: See the Redirect After Login module.

Drupal 7

There are many ways to do this, either by using modules included in Drupal Core; or using any number of different contributed modules; or by building a custom module implementing the Drupal API. "Your mileage may vary" with each. Be warned, the login block sometimes behaves differently than the login page. Also, make sure you test the password reset path which can be affected.

Using Drupal core site-building

With Actions & Triggers

You'll first need to define the URL redirect action at admin/config/system/actions.

Then, after making sure the core module Trigger is enabled at /admin/modules (or using Drush), assign the URL redirect action you just defined to the "After a user has logged in" trigger at /admin/structure/trigger/user.

Subscribe with RSS Subscribe to RSS - after log-in