You are here

public function GoogleAuthController::callback in Social Auth Google 8

Same name and namespace in other branches
  1. 8.2 src/Controller/GoogleAuthController.php \Drupal\social_auth_google\Controller\GoogleAuthController::callback()
  2. 3.x src/Controller/GoogleAuthController.php \Drupal\social_auth_google\Controller\GoogleAuthController::callback()

Callback function to login user.

1 string reference to 'GoogleAuthController::callback'
social_auth_google.routing.yml in ./social_auth_google.routing.yml
social_auth_google.routing.yml

File

src/Controller/GoogleAuthController.php, line 98

Class

GoogleAuthController
Manages requests to Google API.

Namespace

Drupal\social_auth_google\Controller

Code

public function callback() {

  /* @var \Google_Client $client */
  $client = $this->networkManager
    ->createInstance('social_auth_google')
    ->getSdk();
  $this->googleManager
    ->setClient($client)
    ->authenticate();

  // Saves access token so that event subscribers can call Google API.
  $this->session
    ->set('social_auth_google_access_token', $this->googleManager
    ->getAccessToken());

  // Gets user information.
  $user = $this->googleManager
    ->getUserInfo();

  // If user information could be retrieved.
  if ($user) {
    return $this->userManager
      ->authenticateUser($user
      ->getEmail(), $user
      ->getName(), $user
      ->getId(), $user
      ->getPicture());
  }
  drupal_set_message($this
    ->t('You could not be authenticated, please contact the administrator'), 'error');
  return $this
    ->redirect('user.login');
}