You are here

public function SocialAuthUserManager::authenticateUser in Social Auth 8

Creates and/or authenticates an user.

Parameters

string $email: The user's email address.

string $name: The user's name.

string $id: The user's id in provider.

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

Return value

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

File

src/SocialAuthUserManager.php, line 153

Class

SocialAuthUserManager
Contains all logic that is related to Drupal user management.

Namespace

Drupal\social_auth

Code

public function authenticateUser($email, $name, $id = NULL, $picture_url = FALSE) {

  // Tries to load the user by their email.
  $drupal_user = $this
    ->loadUserByProperty('mail', $email);

  // If user email has already an account in the site.
  if ($drupal_user) {

    // Authenticates and redirect existing user.
    return $this
      ->authenticateExistingUser($drupal_user);
  }
  $drupal_user = $this
    ->createUser($name, $email);

  // If the new user could be registered.
  if ($drupal_user) {

    // Download profile picture for the newly created user.
    if ($picture_url) {
      $this
        ->setProfilePic($drupal_user, $picture_url, $id);
    }

    // Authenticates and redirect new user.
    return $this
      ->authenticateNewUser($drupal_user);
  }
  drupal_set_message($this
    ->t('You could not be authenticated, please contact the administrator'), 'error');
  $this
    ->nullifySessionKeys();
  return $this
    ->redirect('user.login');
}