You are here

public function UserAuthenticator::authenticateUser in Social Auth 8.2

Same name and namespace in other branches
  1. 3.x src/User/UserAuthenticator.php \Drupal\social_auth\User\UserAuthenticator::authenticateUser()

Creates and/or authenticates an user.

Parameters

string $name: The user's name.

string $email: The user's email address.

string $provider_user_id: The unique id returned by the user.

string $token: The access token for making additional API calls.

string|null $picture_url: The user's picture.

array|null $data: The additional user data to be stored in database.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse A redirect response.

File

src/User/UserAuthenticator.php, line 117

Class

UserAuthenticator
Manages Drupal authentication tasks for Social Auth.

Namespace

Drupal\social_auth\User

Code

public function authenticateUser($name, $email, $provider_user_id, $token, $picture_url = NULL, $data = NULL) {

  // Checks for record in Social Auth entity.
  $user_id = $this->userManager
    ->getDrupalUserId($provider_user_id);

  // If user is already authenticated.
  if ($this->currentUser
    ->isAuthenticated()) {

    // If no record for provider exists.
    if ($user_id === FALSE) {
      $this
        ->associateNewProvider($provider_user_id, $token, $data);
      return $this->response;
    }
    else {
      return $this
        ->getPostLoginRedirection();
    }
  }

  // If user previously authorized the provider, load user through provider.
  if ($user_id) {
    $this
      ->authenticateWithProvider($user_id);
    return $this->response;
  }

  // Try to authenticate user using email address.
  if ($email) {

    // If authentication with email was successful.
    if ($this
      ->authenticateWithEmail($email, $provider_user_id, $token, $data)) {
      return $this->response;
    }
  }
  $user = new SocialAuthUser($name, $email, $provider_user_id, $token, $picture_url, $data);

  // At this point, create a new user.
  $drupal_user = $this->userManager
    ->createNewUser($user);
  $this
    ->authenticateNewUser($drupal_user);
  return $this->response;
}