17

I am trying to redirect users to a custom URL after they logged in, but my code doesn't work.

How do I redirect users after they logged in?

12

you can use hook_user_login and try to redirect to yourpath

function yourmodule_user_login($account) {
  // We want to redirect user on login.
  $response = new RedirectResponse("yourpath");
  $response->send();
  return;
}

Also you can use Rules module, but there is NOT stable version for Drupal 8 yet.

  • A good elegant solution, worked for me in Drupal 8.1. – Alexei Rayu May 2 '16 at 9:55
  • 3
    I was getting ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION errors in certain circumstances with the above - I suggest replacing return; with exit;, to ensure the redirect - which sets headers - is the last thing to be executed. – BrynJ Jul 14 '16 at 14:19
  • hook_user_login() is called before the submit handlers so this would prevent them from being called. Yogesh's answer below is a better way of doing this. – imclean Oct 1 '18 at 1:29
4

You can also alter the user login form and add your own custom submit handler to set $form_state redirect, instead of directly redirecting user to your custom URL using hook_user_login.

<?php
/**
 * Implements hook_form_alter().
 */
function [MODULENAME]_form_alter(&$form, \Drupal\Core\Form\FormStateInterface\FormStateInterface $form_state, $form_id) {
  switch ($form_id) {
    // Alter login form and add own custom submit handler.
    case 'user_login_form':
      $form['#submit'][] = '_[MODULENAME]_user_login_form_submit';
      break;
  }
}

/**
 * Custom submit handler for login form.
 */
function _[MODULENAME]_user_login_form_submit($form, FormStateInterface $form_state) {
  // Set redirect to login form.
  $form_state->setRedirect('YOUR.MENU-ROUTER.NAME');
}

Adding it in $form_state redirect will make sure that the other submit handlers/login hooks get called.

Like Drupal7 we can't set $form_state['redirect'] directly, because $form_state is now class object. Checkout FormState::setRedirect() for further details.

3

There is a simple module to do this which is compatible with Drupal 8. It is called User Default Page.

The module allows you to customize the destination that a user is redirected to after logging in or logged out. You can customize by roles or individual users. And customize configurable drupal messages for this actions.

0

https://www.drupal.org/project/redirect

  1. Install the above module
  2. Add custom url path as the destination parameter in the url
  3. So, configure the above module to redirect from

    /user/login to /user/login?destination='CUSTOM_NODE_PATH'

    • Wanted to use r4032login module to allow authenticated users access the concerned page, when they copy/paste the url or use an hyperlink from word/excel for example.
    • Other 'login redirect' modules would override this functionality and would only allow users to land on a configured single page even when the destination parameters are included in the url
0

Another option specifically for this issue is a simple Drupal 8 contributed module: Redirect After Login. The module can be found here:

https://www.drupal.org/project/redirect_after_login

As the description shows, it is a simple, focused module that is covered by the Drupal security advisory policy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.