You are here

public function FacebookAuthController::callback in Social Auth Facebook 8.2

Same name and namespace in other branches
  1. 3.x src/Controller/FacebookAuthController.php \Drupal\social_auth_facebook\Controller\FacebookAuthController::callback()

Response for path 'user/login/facebook/callback'.

Facebook returns the user here after user has authenticated.

1 string reference to 'FacebookAuthController::callback'
social_auth_facebook.routing.yml in ./social_auth_facebook.routing.yml
social_auth_facebook.routing.yml

File

src/Controller/FacebookAuthController.php, line 71

Class

FacebookAuthController
Returns responses for Social Auth Facebook routes.

Namespace

Drupal\social_auth_facebook\Controller

Code

public function callback() {

  // Checks if there was an authentication error.
  $redirect = $this
    ->checkAuthError();
  if ($redirect) {
    return $redirect;
  }

  /* @var \League\OAuth2\Client\Provider\FacebookUser|null $profile */
  $profile = $this
    ->processCallback();

  // If authentication was successful.
  if ($profile) {

    // Check for email.
    if (!$profile
      ->getEmail()) {
      $this->messenger
        ->addError($this
        ->t('Facebook authentication failed. This site requires permission to get your email address.'));
      return $this
        ->redirect('user.login');
    }

    // Gets (or not) extra initial data.
    $data = $this->userAuthenticator
      ->checkProviderIsAssociated($profile
      ->getId()) ? NULL : $this->providerManager
      ->getExtraDetails();

    // If user information could be retrieved.
    return $this->userAuthenticator
      ->authenticateUser($profile
      ->getName(), $profile
      ->getEmail(), $profile
      ->getId(), $this->providerManager
      ->getAccessToken(), $profile
      ->getPictureUrl(), $data);
  }
  return $this
    ->redirect('user.login');
}