When a user logs into Drupal with hybridauth, the old session data is loaded from the database and merged with the newest session information. But the data from the current session isn't written to storage until the user logs out.

This is a problem if a service wants to load information about a user's session or auth tokens. They would get the old tokens which have expired.

I would suggest that the hook_user_login() be modified so that right after we restore the session, we save that new session information to the database.

/**
 * Implements hook_user_login().
 */
function hybridauth_user_login(&$edit, $account) {
  if ($hybridauth_session_data = _hybridauth_session_load_by_uid($account->uid)) {
    if ($hybridauth = hybridauth_get_instance()) {
      if (is_object($hybridauth)) {
        $hybridauth_session_data = $hybridauth_session_data['data'];
        $hybridauth_session_data_current = unserialize($hybridauth->getSessionData());
        if (is_array($hybridauth_session_data_current)) {
          $hybridauth_session_data = serialize(array_merge(unserialize($hybridauth_session_data), $hybridauth_session_data_current));
        }
        // Call Hybrid_Auth::restoreSessionData() to set stored data.
        $hybridauth->restoreSessionData($hybridauth_session_data);
       }
    }
  }
}

to

/**
 * Implements hook_user_login().
 */
function hybridauth_user_login(&$edit, $account) {
  if ($hybridauth_session_data = _hybridauth_session_load_by_uid($account->uid)) {
    if ($hybridauth = hybridauth_get_instance()) {
      if (is_object($hybridauth)) {
        $hybridauth_session_data = $hybridauth_session_data['data'];
        $hybridauth_session_data_current = unserialize($hybridauth->getSessionData());
        if (is_array($hybridauth_session_data_current)) {
          $hybridauth_session_data = serialize(array_merge(unserialize($hybridauth_session_data), $hybridauth_session_data_current));
        }
        // Call Hybrid_Auth::restoreSessionData() to set stored data.
        $hybridauth->restoreSessionData($hybridauth_session_data);
        _hybridauth_session_save($hybridauth_session_data, $account->uid);
      }
    }
  }
}

This pattern also follows the example session handling documentation for hybridauth itself.
http://hybridauth.sourceforge.net/userguide/HybridAuth_Sessions.html

Note that when creating a session, they immediately store that information as well.

CommentFileSizeAuthor
#2 2626624-session.patch447 bytesminoroffense

Comments

minorOffense created an issue. See original summary.

minoroffense’s picture

Status: Active » Needs review
StatusFileSize
new447 bytes

Here's a patch with the suggested changes

duozersk’s picture

Status: Needs review » Fixed

Mathew,

Thank you for using the module and for the patch.

Committed!

There might also be some other places to refresh the session info... like when the user adds new identity to their account; and may be smth else.

Thanks
AndyB

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.