You are here

function simple_fb_connect_get_session in Simple FB Connect 7.2

Loads the user's access token from $_SESSION and validates it.

Return value

FacebookSession object if access token was found and session is valid. FALSE otherwise.

4 calls to simple_fb_connect_get_session()
hook_simple_fb_connect_registration in ./simple_fb_connect.api.php
This hook allows other modules to act on user creation via Facebook login.
simple_fb_connect_return_from_fb in ./simple_fb_connect.module
Page callback for /user/simple-fb-connect/return.
simple_fb_connect_user_has_facebook_session in ./simple_fb_connect.rules.inc
Rule condition: Check if user has a valid Facebook session.
simple_fb_connect_user_logout in ./simple_fb_connect.module
Implemets hook_user_logout().

File

./simple_fb_connect.module, line 376
Simple Facebook Login Module for Drupal Sites.

Code

function simple_fb_connect_get_session() {
  static $fb_session = NULL;

  // We are only executing the following code when this function is called for the first time.
  if (is_null($fb_session)) {
    if (isset($_SESSION['simple_fb_connect']['user_token'])) {
      $user_token = $_SESSION['simple_fb_connect']['user_token'];
      try {

        // Check that we are able to start a session with the token.
        $fb_session = new FacebookSession($user_token);
        $fb_session
          ->validate();
        return $fb_session;
      } catch (FacebookRequestException $ex) {

        // Session not valid, Graph API returned an exception with the reason.
        watchdog('simple_fb_connect', 'Could not load FB session. FacebookRequestException: @message', array(
          '@message' => json_encode($ex
            ->getResponse()),
        ), WATCHDOG_NOTICE);
      } catch (\Exception $ex) {

        // Graph API returned info, but it may mismatch the current app or have expired.
        watchdog('simple_fb_connect', 'Could not load FB session. Exception: @message', array(
          '@message' => $ex
            ->getMessage(),
        ), WATCHDOG_ERROR);
      }
    }

    // Return FALSE if we don't have a session at all or if the access token was not valid.
    $fb_session = FALSE;
  }
  return $fb_session;
}