How to log user in when in the middle of Drupal Commerce Checkout I have created a commerce checkout page which, under certain conditions, is presented to the user with a custom login screen

$form['username'] = array(
    '#title' => t('Username'),
    '#type' => 'textfield',
    '#size' => 30
  );
  $form['password'] = array(
    '#title' => t('Password'),
    '#type' => 'password',
    '#size' => 30,
  );

In the submit handler, I log the user in

$username = $form_state['values']['user_login']['username'];
  $password = $form_state['values']['user_login']['password'];
  $login_array = array($username, $password);
  if($uid = user_authenticate($username, $password)) {
      global $user;
      $user = user_load($uid);

      $login_array = array ('name' => $username);
      user_login_finalize($login_array);
    }
  else {
    drupal_set_message('Invalid username or password');
  }  

However, what happens is, is on next page, /checkout/order-id/review, it says the user is logged in but page not found. This is because the newly logged in user doesn't have anything in his cart. Is there an easy way of transfering the old cart to this new user? I see that there is a Rule - Assign an anonymous order to a pre-existing user, which I enabled and thought it would do the trick, but it doesn't

In case it helps: there is one rule defined by commerce -- Create a new account for an anonymous order -- which I disabled because I don't want that functionality. I don't know why this would effect anything, but just mentioning it in case it does.
I ended up simply using commerce_cart_order_convert($order, $user); I was able to assign the order to the correct user, and I decided not to log him in at point in time. If I really need to log him in, I will do so after checkout is over when I don't need to access the checkout review page anymore.