Why is redirect after login not working? I use Ctools Modal to show the login form to users in an overlay. If a user enters right data then he is logged in and the page is refreshed.

The code:

function login_overlay_menu() {
  $items['login-overlay'] = array(
    'title' =&gt; t('Sign in'),
    'page callback' =&gt; 'login_overlay_callback',
    'page arguments' =&gt; array(),
    'type' =&gt; MENU_CALLBACK,
    'access callback' =&gt; TRUE,
  );
  return $items;
}

function login_overlay_callback($js = TRUE){
    ctools_include('modal');
    ctools_include('ajax');
    $form_state = array(
      'ajax' =&gt; TRUE,
    );
    $output = ctools_modal_form_wrapper('login_overlay_form', $form_state);
    if (isset($_GET['destination'])) {
        $output[] = login_overlay_redirect($_GET['destination']);
    }
    else {
        $output[] = ctools_ajax_command_reload();
    }
    print ajax_render($output);
}

function login_overlay_redirect($path) {
  global $user;      
  if (isset($languages[$user-&gt;language])) {
    $options = array('language' =&gt; $languages[$user-&gt;language]);
  }
  return ctools_ajax_command_redirect($path, 0, $options);
}


The Catch:

It works well for right data entered at first attempt. However, if a validation fails, then the form is rebuild inside the overlay. and then if the user submits the form, he is logged in but the page is not refreshed (which is very important).

When I debugged the JS code I found that the value of Drupal.settings.ajaxPageState.js changing is the only difference before and after the validation fail.

Before: it is an object containing list of all js files loaded for that page.
After: it is an empty array. (Array[0]).

THE QUESTION:
I am not even sure if this is what causing the error. However if it is, then what may be causing this to happen? OR if not then what may be causing the page refresh to fail?
You can use this hook:

/**
* Implements hook_user_login().
*/
function my_module_user_login(&amp;$edit, $account) {
 // $edit is empty with one time login
 if (!empty($edit)) {
   $_GET['destination'] = 'some-url';
 }
 // Or the form id is the password reset form
 if ($_POST['form_id'] != 'user_pass_reset') {
  $_GET['destination'] = 'some-url';
 }
}