protected function addCookieToResponse(
        ServerRequestInterface $request,
        ResponseInterface $response,
        StoreContract $session
    ): ResponseInterface {
        if ($session->getHandler() instanceof CookieSessionHandler) {
            $session->save();
        }

        $uri    = $request->getUri();

        $setCookie = new SetCookie(
            $session->getName(),
            $session->getId(),
            $this->cookieConfig['expire_on_close'] === true ? 0 : Chronos::now()->addSeconds($this->lifetime),
            $this->cookieConfig['path'] ?? '/',
            $this->cookieConfig['domain'] ?? $uri->getHost(),
            $this->cookieConfig['secure'] ?? ($uri->getScheme() === 'https'),
            $this->cookieConfig['http_only'] ?? true,
            $this->cookieConfig['samesite'] ?? false
        );

        return $response->withAddedHeader('set-cookie', (string) $setCookie);
    }