You are here

public function GoogleAuthController::userRegisterCallback in Open Social 8.5

Same name and namespace in other branches
  1. 8.9 modules/custom/social_auth_google/src/Controller/GoogleAuthController.php \Drupal\social_auth_google\Controller\GoogleAuthController::userRegisterCallback()
  2. 8 modules/custom/social_auth_google/src/Controller/GoogleAuthController.php \Drupal\social_auth_google\Controller\GoogleAuthController::userRegisterCallback()
  3. 8.2 modules/custom/social_auth_google/src/Controller/GoogleAuthController.php \Drupal\social_auth_google\Controller\GoogleAuthController::userRegisterCallback()
  4. 8.3 modules/custom/social_auth_google/src/Controller/GoogleAuthController.php \Drupal\social_auth_google\Controller\GoogleAuthController::userRegisterCallback()
  5. 8.4 modules/custom/social_auth_google/src/Controller/GoogleAuthController.php \Drupal\social_auth_google\Controller\GoogleAuthController::userRegisterCallback()
  6. 8.6 modules/custom/social_auth_google/src/Controller/GoogleAuthController.php \Drupal\social_auth_google\Controller\GoogleAuthController::userRegisterCallback()
  7. 8.7 modules/custom/social_auth_google/src/Controller/GoogleAuthController.php \Drupal\social_auth_google\Controller\GoogleAuthController::userRegisterCallback()
  8. 8.8 modules/custom/social_auth_google/src/Controller/GoogleAuthController.php \Drupal\social_auth_google\Controller\GoogleAuthController::userRegisterCallback()

Registers the new account after redirect from Google.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse Returns a RedirectResponse.

1 string reference to 'GoogleAuthController::userRegisterCallback'
social_auth_google.routing.yml in modules/custom/social_auth_google/social_auth_google.routing.yml
modules/custom/social_auth_google/social_auth_google.routing.yml

File

modules/custom/social_auth_google/src/Controller/GoogleAuthController.php, line 139

Class

GoogleAuthController
Class GoogleAuthController.

Namespace

Drupal\social_auth_google\Controller

Code

public function userRegisterCallback() {
  $sdk = $this
    ->getSdk('register');
  if ($sdk instanceof RedirectResponse) {
    return $sdk;
  }
  $this->authManager
    ->setSdk($sdk);
  $profile = $this
    ->getProfile('register');
  if ($profile instanceof RedirectResponse) {
    return $profile;
  }

  // Check whether user account exists. If account already exists,
  // authorize the user and redirect him to the account page.
  $account = $this
    ->entityTypeManager()
    ->getStorage('user')
    ->loadByProperties([
    'google_id' => $profile
      ->getId(),
  ]);
  if ($account) {
    $account = current($account);
    if (!$account
      ->get('status')->value) {
      drupal_set_message($this
        ->t('You already have account on this site, but your account is blocked. Contact the site administrator.'), 'error');
      return $this
        ->redirect('user.register');
    }
    user_login_finalize($account);
    return $this
      ->redirect('entity.user.canonical', [
      'user' => $account
        ->id(),
    ]);
  }

  // Save email and name to session to use for auto fill the registration
  // form.
  $data_handler = $this->networkManager
    ->createInstance('social_auth_google')
    ->getDataHandler();
  $data_handler
    ->set('access_token', $this->accessToken);
  $data_handler
    ->set('mail', $profile
    ->getEmail());
  $data_handler
    ->set('name', $profile
    ->getName());
  drupal_set_message($this
    ->t('You are now connected with @network, please continue registration', [
    '@network' => $this
      ->t('Google'),
  ]));
  return $this
    ->redirect('user.register', [
    'provider' => 'google',
  ]);
}