You are here

public function TokenHandler::setTokenOnResponseEvent in Persistent Login 8

Set or clear a token cookie on this response, if required.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event: The response event.

File

src/EventSubscriber/TokenHandler.php, line 143

Class

TokenHandler
Class TokenHandler.

Namespace

Drupal\persistent_login\EventSubscriber

Code

public function setTokenOnResponseEvent(FilterResponseEvent $event) {
  if (!$event
    ->isMasterRequest()) {
    return;
  }
  if ($this->token) {
    $request = $event
      ->getRequest();
    $response = $event
      ->getResponse();
    $sessionOptions = $this->sessionConfiguration
      ->getOptions($request);
    if ($this->token
      ->getStatus() === PersistentToken::STATUS_VALID) {

      // New or updated token.
      $this->token = $this->tokenManager
        ->updateToken($this->token);
      $response->headers
        ->setCookie(new Cookie($this->cookieHelper
        ->getCookieName($request), $this->token, $this->token
        ->getExpiry(), '/', $sessionOptions['cookie_domain'], $sessionOptions['cookie_secure']));
      $response
        ->setPrivate();
    }
    elseif ($this->token
      ->getStatus() === PersistentToken::STATUS_INVALID) {

      // Invalid token, or manually cleared token (e.g. user logged out).
      $this->tokenManager
        ->deleteToken($this->token);
      $response->headers
        ->clearCookie($this->cookieHelper
        ->getCookieName($request), '/', $sessionOptions['cookie_domain'], $sessionOptions['cookie_secure']);
      $response
        ->setPrivate();
    }
    else {

      // Ignore token if status is STATUS_NOT_VALIDATED.
    }
  }
}