1

I want to redirect all the users to a specific page after login. It seems like an easy thing to do using rules.

Event
User has logged in

Action
Page redirect

There is a small problem though. Page redirection will also work on the first login/password recovery scenario which in practice disallows the user to set/change his password.

Any ideas how can I deal with this issue?

I have found that the Login Destination module seems to be able to solve my problems, but I would prefer not to add more modules if it is unnecessary (I have enough things to debug now).

  • you can set rules for authenticated and non-authenticated users separately and can redirect non-authenticated users to registration page and authenticated to where ever you want . – Prerit Mohan Apr 26 '13 at 10:35
  • Prerit you misunderstood. I am talking about authenticated users after logging in. The problem is that the single use login link triggers "User has logged in" event and by redirection I break its functionality. – Pawel G Apr 26 '13 at 10:45
  • oh all right!!! – Prerit Mohan Apr 26 '13 at 11:03
1

Would something like the following work?

{ "rules_after_login_redirect_to_page" : {
    "LABEL" : "After login redirect to page.",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "0",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "user_login" ],
    "IF" : [
      { "NOT text_matches" : {
          "text" : [ "site:current-page:path" ],
          "match" : "^user\/reset\/",
          "operation" : "regex"
        }
      }
    ],
    "DO" : [ { "redirect" : { "url" : "your_page_url" } } ]
  }
}
  • This seems like the way to go. [site:current-page:path] when logging in normally returns "user/login", when I use the reset-password link I get "user/reset/.../.../.../login". – Pawel G Apr 26 '13 at 11:18
1

Use the $account object's access property to check if the user has logged in before, in hook_user_login.

function MODULE_user_login(&$edit, $account) {
  if ($account->access == 0) {
    // Account never accessed
  }
  elseif (arg(0) == 'user' && arg(1) == 'reset') {
    // User on password reset page
  }
  else {
    // User has logged in before.
    drupal_goto('<redirection/path>');
  }
}
  • "Account never accessed" condition is unnecessary here. arg==reset takes care of all I need. Thanks for the answer. – Pawel G Apr 26 '13 at 11:31
1

drupal_goto() in general is not the good way to go in form handling because it stops all hook execution after it , and you may dont want that to happen...

The drupal way is to fully control your login form(s) and their submit handlers.You just have to add your own submit handler to the form

<?php
/**
 * Implements hook_form_alter().
 */
function MODULE_form_alter(&$form, &$form_state, $form_id) {
  // Take care the login block also
  $forms_to_alter = array(
    'user_login', 'user_login_block',
  );
  // Add your own submit handler to the default ones
  if (in_array($form_id, $forms_to_alter)) {
    $form['#submit'][] = '_my_login_submit';
  }
}

And then all you need is to change the $form_state['redirect'] in there like:

function _my_login_submit($form, &$form_state) {
  $form_state['redirect'] = 'your_other-path';
}

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.