You are here

FacebookAuthController.php in Social Auth Facebook 3.x

File

src/Controller/FacebookAuthController.php
View source
<?php

namespace Drupal\social_auth_facebook\Controller;

use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\social_api\Plugin\NetworkManager;
use Drupal\social_auth\Controller\OAuth2ControllerBase;
use Drupal\social_auth\SocialAuthDataHandler;
use Drupal\social_auth\User\UserAuthenticator;
use Drupal\social_auth_facebook\FacebookAuthManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Returns responses for Social Auth Facebook routes.
 */
class FacebookAuthController extends OAuth2ControllerBase {

  /**
   * FacebookAuthController constructor.
   *
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   * @param \Drupal\social_api\Plugin\NetworkManager $network_manager
   *   Used to get an instance of social_auth_facebook network plugin.
   * @param \Drupal\social_auth\User\UserAuthenticator $user_authenticator
   *   Used to manage user authentication/registration.
   * @param \Drupal\social_auth_facebook\FacebookAuthManager $facebook_manager
   *   Used to manage authentication methods.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request
   *   Used to access GET parameters.
   * @param \Drupal\social_auth\SocialAuthDataHandler $data_handler
   *   The Social Auth Data handler.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   Used to handle metadata for redirection to authentication URL.
   */
  public function __construct(MessengerInterface $messenger, NetworkManager $network_manager, UserAuthenticator $user_authenticator, FacebookAuthManager $facebook_manager, RequestStack $request, SocialAuthDataHandler $data_handler, RendererInterface $renderer) {
    parent::__construct('Social Auth Facebook', 'social_auth_facebook', $messenger, $network_manager, $user_authenticator, $facebook_manager, $request, $data_handler, $renderer);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('messenger'), $container
      ->get('plugin.network.manager'), $container
      ->get('social_auth.user_authenticator'), $container
      ->get('social_auth_facebook.manager'), $container
      ->get('request_stack'), $container
      ->get('social_auth.data_handler'), $container
      ->get('renderer'));
  }

  /**
   * Response for path 'user/login/facebook/callback'.
   *
   * Facebook returns the user here after user has authenticated.
   */
  public function callback() {

    // Checks if there was an authentication error.
    $redirect = $this
      ->checkAuthError('error_code');
    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');
  }

}

Classes

Namesort descending Description
FacebookAuthController Returns responses for Social Auth Facebook routes.