You are here

public function UserAuthenticator::authenticateWithEmail in Social Auth 3.x

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

Authenticates user by email address.

Parameters

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.

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

Return value

bool True if user could be authenticated with email. False otherwise.

1 call to UserAuthenticator::authenticateWithEmail()
UserAuthenticator::authenticateUser in src/User/UserAuthenticator.php
Creates and/or authenticates an user.

File

src/User/UserAuthenticator.php, line 232

Class

UserAuthenticator
Manages Drupal authentication tasks for Social Auth.

Namespace

Drupal\social_auth\User

Code

public function authenticateWithEmail($email, $provider_user_id, $token, $data) {
  try {

    // Load user by email.
    $drupal_user = $this->userManager
      ->loadUserByProperty('mail', $email);

    // Check if user with same email account exists.
    if ($drupal_user) {

      // Add record for the same user.
      $this->userManager
        ->addUserRecord($drupal_user
        ->id(), $provider_user_id, $token, $data);

      // Authenticates and redirect the user.
      $this
        ->authenticateExistingUser($drupal_user);
      return TRUE;
    }
  } catch (\Exception $ex) {
    $this->loggerFactory
      ->get($this
      ->getPluginId())
      ->error('Failed to authenticate user. Exception: @message', [
      '@message' => $ex
        ->getMessage(),
    ]);
  }
  return FALSE;
}