Categories
CMS Development Magento

Session not set in PHPSESSID Cookie when FPC is enabled

I am trying to pro-grammatically log in using the SID. The front-end already shows me that I am logged in, but as soon as I go to a page that needs a logged in user (ex. account page) I am not actually logged in. I figured out it’s because the PHPSESSID cookie is not matching the SID.
Once I clear the PHPSESSID cookie (with $this->expireSessionCookie()) the SID is properly set in the PHPSESSID cookie. This only works though if Full Page Cache is disabled and is obviously just a workaround…

Shouldn’t parent::setSessionId($sessionId); set the cookie correctly?

Any help would be greatly appreciated.

namespace Acme\SessionTest\Model;

class Core extends \Magento\Framework\Session\Generic
{
    /**
     * @var \Magento\Framework\Session\SidResolverInterface
     */
    protected $sidResolver;
    protected $appState;
    protected $request;
    protected $cookieMetadataFactory;

public function __construct(
    \Magento\Framework\App\Request\Http $request,
    \Magento\Framework\Session\SidResolverInterface $sidResolver,
    \Magento\Framework\Session\Config\ConfigInterface $sessionConfig,
    \Magento\Framework\Session\SaveHandlerInterface $saveHandler,
    \Magento\Framework\Session\ValidatorInterface $validator,
    \Magento\Framework\Session\StorageInterface $storage,
    \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
    \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
    \Magento\Framework\App\State $appState
) {
    $this->sidResolver = $sidResolver;
    $this->appState = $appState;
    $this->request = $request;
    $this->cookieMetadataFactory = $cookieMetadataFactory;
    parent::__construct(
        $request,
        $sidResolver,
        $sessionConfig,
        $saveHandler,
        $validator,
        $storage,
        $cookieManager,
        $cookieMetadataFactory,
        $appState
    );
}

public function setSessionId($sessionId = NULL)
{ 
    if (is_null($sessionId)){
        $this->expireSessionCookie();

        $_queryParam = $this->sidResolver->getSessionIdQueryParam($this);
        if($this->request->getParam($_queryParam)){
            $sessionId = $this->request->getParam($_queryParam);
        }

    }
    return parent::setSessionId($sessionId);
}

Leave a Reply

Your email address will not be published. Required fields are marked *