You are here

public function UserAuth::authenticate in farmOS 2.x

Validates user authentication credentials.

Parameters

string $username: The user name to authenticate.

string $password: A plain-text password, such as trimmed text from form values.

Return value

int|bool The user's uid on success, or FALSE on failure to authenticate.

Overrides UserAuth::authenticate

File

modules/core/login/src/UserAuth.php, line 15

Class

UserAuth
Extends the core user.auth service to load users by their email.

Namespace

Drupal\farm_login

Code

public function authenticate($username, $password) {
  $uid = parent::authenticate($username, $password);

  // If the parent failed to authenticate, try loading the user by email.
  if (empty($uid) && !empty($username) && strlen($password) > 0) {
    $account_search = $this->entityTypeManager
      ->getStorage('user')
      ->loadByProperties([
      'mail' => $username,
    ]);
    if ($account = reset($account_search)) {
      if ($this->passwordChecker
        ->check($password, $account
        ->getPassword())) {

        // Successful authentication.
        $uid = $account
          ->id();

        // Update user to new password scheme if needed.
        if ($this->passwordChecker
          ->needsRehash($account
          ->getPassword())) {
          $account
            ->setPassword($password);
          $account
            ->save();
        }
      }
    }
  }
  return $uid;
}