0
votes

I'm using Drupal 6.

I have a page with a "login" link that pops up a JQuery lightbox "popup" (really just an overlayed div that displays the content of another page) for the user to login with. That pop up contains the text of the page /login/, which is basically just:

<div id="login">
<?php print drupal_get_form('user_login_block'); ?>
</div>

I have a user_login.tpl.php that I'm using to style the login form.

When the user submits the login form I'd like to redirect them back to the homepage, whether they log in successfully or not. I should just be able to display $messages on that page to let them know whether they're logged in or if they entered their password wrong, etc. At the moment, if a user enters their login details and submits then they get taken (via the form action) through to /login/, which isn't a "real" page (since it just contains the content of the "popup").

I've added a function to change the form redirect back to the homepage:

function mr_misc_hooks_form_user_login_block_alter(&$form, &$form_state/*, $form_id*/) {
/* Drupal will ignore redirects we set for the login block unless we do the next line.
 * See http://drupal.org/node/580144#comment-3368072 
 * I spent a *lot* of time trying to work this out. :( */
unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);
$form['#redirect'] = 'node';
}

This successfully redirects the user on a submit that succeeds - i.e. when the submit ends up logging in the user. It doesn't do any redirection on a submit that fails.

How can I redirect users back to the homepage when their login is unsuccessful?

1

1 Answers

0
votes

Here's an idea for another approach. Sorry I don't have time to test or confirm it works though.

<?php
// add a custom submit handler for the user_login form
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_login') {
    $form['#submit'][] = 'mymodule_myloginhandler';
  }
}

// set message based on if user was logged in, and then the redirect
function mymodule_myloginhandler($form, &$form_state) {
  global $user;

  if ($user->uid) {
    drupal_set_message('Login Successful');
  }
  else {
    drupal_set_message('Invalid login.');
  }

  $form_state['redirect'] = 'node';
}
?>