You are here

GoogleAuthController.php in Social Auth Google 8.2

File

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

namespace Drupal\social_auth_google\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_google\GoogleAuthManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Returns responses for Social Auth Google module routes.
 */
class GoogleAuthController extends OAuth2ControllerBase {

  /**
   * GoogleAuthController 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_google network plugin.
   * @param \Drupal\social_auth\User\UserAuthenticator $user_authenticator
   *   Used to manage user authentication/registration.
   * @param \Drupal\social_auth_google\GoogleAuthManager $google_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, GoogleAuthManager $google_manager, RequestStack $request, SocialAuthDataHandler $data_handler, RendererInterface $renderer) {
    parent::__construct('Social Auth Google', 'social_auth_google', $messenger, $network_manager, $user_authenticator, $google_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_google.manager'), $container
      ->get('request_stack'), $container
      ->get('social_auth.data_handler'), $container
      ->get('renderer'));
  }

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

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

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

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

      // Gets (or not) extra initial data.
      $data = $this->userAuthenticator
        ->checkProviderIsAssociated($profile
        ->getId()) ? NULL : $this->providerManager
        ->getExtraDetails();
      return $this->userAuthenticator
        ->authenticateUser($profile
        ->getName(), $profile
        ->getEmail(), $profile
        ->getId(), $this->providerManager
        ->getAccessToken(), $profile
        ->getAvatar(), $data);
    }
    return $this
      ->redirect('user.login');
  }

}

Classes

Namesort descending Description
GoogleAuthController Returns responses for Social Auth Google module routes.