Magento2: After enable full page cache customer session data not found.

Created on 12 Jul 2016  路  12Comments  路  Source: magento/magento2

Steps to reproduce

  1. Magento 210
  2. Magento Backend System-> Cache Management -> enable all cache type enable.

    Expected result

  3. if($customerSession->isLoggedIn()) {
    echo "logged in Customer";
    }else
    {
    echo "Not Logged In Customer";
    }
    After enable cache every time if condition not true .

  4. ...

    Actual result

  5. It should be return true in customer logged in case.

Customer Format is not valid needs update bug report

All 12 comments

Please, format this issue according to the Issue reporting guidelines.

The contributor guide suggests that tickets that have not been active for two weeks should be closed. Please reopen as needed.

To add to this I am able to replicate:
Preconditions:
Magento Version CE 2.1.1
PHP version 7.0.7
Full Page Caching Enabled
Chrome Version 53.0.2785.143 m

Steps to reproduce with Actual and Expected Result
When Full Page Caching is enabled this always results $customerId as null, disabling full page caching means the customerId is correctly returned.

$customerId = $this->_customerSession->getId();
            /**
             * Check the user is logged in
             */
            if (!$customerId) {
                return "User not logged in";
            }

This appears to only happen in Google Chrome. Firefox works as expected.

me also face same issue. is there any result ?

I have also this issue. I try to add some custome values in CustomSession, but when I do a getCustomValue, I have nothing.
If I disable Fulle page, it's OK.
Have you an explanation please ?

Thank you.

Same here.

Setting a session using \Magento\Framework\Session\Generic on a custom Controller returns NULL on the Block when cache is enabled. With cache disabled it works fine.

I am having the same problem in Magento 2.2.1 as well .. I tried following code in a .phtml file...

$om = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $om->get('MagentoCustomerModelSession');
echo $customerSession->isLoggedIn();

it did not return anything when Page Cache is ON, and when OFF I can see a value (1) printed.

If you have a custom template just put: cacheable="false" in ypur block declaration in layout: example:

<block class="Custom\Theme\Block\Customheader" name="custom_header" as="custom_header" template="Magento_Theme::html/customheader.phtml" before="-" cacheable="false" />

You can try by creating an object of objectManager and should not use objectManager directly.

Use something like,

class Example extends \Magento\Framework\View\Element\Template
{
    private $_objectManager;
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectmanager
    ){
        $this->_objectManager = $objectmanager;
    }

    public function getExample()
    {
        $customerSession = $this->_objectManager->create("Magento\Customer\Model\Session");
        if ($customerSession->isLoggedIn()) {
            $customerData = $customerSession->getCustomer()->getData();
            /*Your logic*/
        }
    }
}

I have already added cacheable="false" but it is not working in my custom block.
I have to clear cache then it is working.

Was this page helpful?
0 / 5 - 0 ratings