I have coded a module which uses hook_user at login to set some user variables. When using masquerade to switch user, hook_user is not called and my variables are not set.

I think a "hook_user('load', $new_account)" call whould be the solution.

Comments

Title:call hook_user when switchingsimulate user login when switching

Yes, this would be great. I have some rules that set user roles at login depending on various conditions, and to check how it all works via masquerade simulating user login is needed. Obviously, hook_user('login') should be called somewhere.

@enboig and @gease I am interested in your use of login and rules for possible use in http://drupal.org/node/1416136. The second login has to get all the roles assigned to the fist login. I am looking at custom code. hook_user and/or Rules might be a cleaner way to do things.

At hook_boot() I check global $user->uid versus $_SESSION['my_old_uid']; if its different, I reload anything I need and update $_SESSION['my_old_uid'].

function my_module_boot() {
  global $user;
  if ($user->uid <> $_SESSION['mymodule_old_uid']) {
    //my code to reload data
    $_SESSION['mymodule_old_uid']=$user->uid;
  }
}

Sounds like your code would have to run after everything else that modifies the user. For my use, it would be something like:
$old = load user $_SESSION['mymodule_old_uid']
$user->roles = array_merge($user->roles, $old->roles);