function LoginFinalizer::finalizeLogin

Same name and namespace in other branches
  1. 11.x core/modules/user/src/LoginFinalizer.php \Drupal\user\LoginFinalizer::finalizeLogin()

Finalize the login process and logs in a user.

This method logs in the user, records a watchdog message about the new session, saves the login timestamp, calls hook_user_login(), and generates a new session.

The current user is replaced with the passed in account.

Parameters

\Drupal\user\UserInterface $user: The user account to log in.

File

core/modules/user/src/LoginFinalizer.php, line 42

Class

LoginFinalizer
Finalize user login.

Namespace

Drupal\user

Code

public function finalizeLogin(UserInterface $user) : void {
  $this->accountProxy
    ->setAccount($user);
  $this->logger
    ->info('Session opened for %name.', [
    '%name' => $user->getAccountName(),
  ]);
  // Update the user table timestamp noting user has logged in.
  // This is also used to invalidate one-time login links.
  $user->setLastLoginTime($this->time
    ->getRequestTime());
  $this->entityTypeManager
    ->getStorage('user')
    ->updateLastLoginTimestamp($user);
  // Regenerate the session ID to prevent against session fixation attacks.
  // This is called before hook_user_login() in case one of those functions
  // fails or incorrectly does a redirect which would leave the old session
  // in place.
  $session = $this->requestStack
    ->getSession();
  $session->migrate();
  $session->set('uid', $user->id());
  $session->set('check_logged_in', TRUE);
  $this->moduleHandler
    ->invokeAll('user_login', [
    $user,
  ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.