You are here

public function SimpleFbConnectController::returnFromFb in Simple FB Connect 8.2

Same name and namespace in other branches
  1. 8.3 src/Controller/SimpleFbConnectController.php \Drupal\simple_fb_connect\Controller\SimpleFbConnectController::returnFromFb()

Response for path 'user/simple-fb-connect/return'.

Facebook returns the user here after user has authenticated in FB.

1 string reference to 'SimpleFbConnectController::returnFromFb'
simple_fb_connect.routing.yml in ./simple_fb_connect.routing.yml
simple_fb_connect.routing.yml

File

src/Controller/SimpleFbConnectController.php, line 78
Contains \Drupal\simple_fb_connect\Controller\SimpleFbConnectController.

Class

SimpleFbConnectController
Returns responses for Simple FB Connect module routes.

Namespace

Drupal\simple_fb_connect\Controller

Code

public function returnFromFb() {

  // Validate configuration.
  if (!$this->fbManager
    ->validateConfig()) {
    drupal_set_message(t('Simple FB Connect not configured properly.'), 'error');
    return $this
      ->redirect('user.login');
  }

  // SDK can start FacebookSession from the page where FB returned the user.
  $login_helper = new FacebookRedirectLoginHelper($this->fbManager
    ->getReturnUrl());
  if (!$this->fbManager
    ->startFbSession($login_helper)) {
    drupal_set_message(t("Facebook login failed."), 'error');
    return $this
      ->redirect('user.login');
  }

  // Get a validated FacebookSession object.
  if (!($fb_session = $this->fbManager
    ->getFbSession())) {
    drupal_set_message(t("Facebook login failed."), 'error');
    return $this
      ->redirect('user.login');
  }

  // Get user's FB profile from Facebook API.
  if (!($fb_profile = $this->fbManager
    ->getFbProfile($fb_session))) {
    drupal_set_message(t("Facebook login failed, could not load Facebook profile. Contact site administrator."), 'error');
    return $this
      ->redirect('user.login');
  }

  // Get user's email from the FB profile.
  if (!($email = $this->fbManager
    ->getEmail($fb_profile))) {
    drupal_set_message(t('Facebook login failed. This site requires permission to get your email address.'), 'error');
    return $this
      ->redirect('user.login');
  }

  // If we have an existing user with the same email address, try to log in.
  if ($drupal_user = $this->userManager
    ->loadUserByProperty('mail', $email)) {
    if ($this->userManager
      ->loginUser($drupal_user)) {
      return new RedirectResponse($this->postLoginManager
        ->getPostLoginPath());
    }
    else {
      return $this
        ->redirect('user.login');
    }
  }

  // If there was no existing user, try to create a new user.
  $drupal_user = $this->userManager
    ->createUser($fb_profile
    ->getProperty('name'), $email);
  if ($drupal_user) {

    // Download profile picture for the newly created user.
    if ($picture_url = $this->fbManager
      ->getFbProfilePicUrl($fb_session)) {
      $this->userManager
        ->setProfilePic($drupal_user, $picture_url, $fb_profile
        ->getProperty('id'));
    }

    // Log the newly created user in.
    if ($this->userManager
      ->loginUser($drupal_user)) {

      // Check if new users should be redirected to Drupal user form.
      if ($this->postLoginManager
        ->getRedirectNewUsersToUserFormSetting()) {
        drupal_set_message(t("Please check your account details. Since you logged in with Facebook, you don't need to update your password."));
        return new RedirectResponse($this->postLoginManager
          ->getPathToUserForm($drupal_user));
      }

      // Use normal post login path if user wasn't redirected to user form.
      return new RedirectResponse($this->postLoginManager
        ->getPostLoginPath());
    }
    else {

      // New user was succesfully created but the account is pending approval.
      drupal_set_message(t('You will receive an email when site administrator activates your account.'), 'warning');
      return $this
        ->redirect('user.login');
    }
  }
  else {

    // User could not be created.
    return $this
      ->redirect('user.login');
  }

  // This should never be reached, user should have been redirected already.
  throw new AccessDeniedHttpException();
}