Chapters Close

How to check customer is logged in or not when a cache is enabled in Magento 2

In this article, you will be learning how to check customer is logged in or not when a cache is enabled in Magento 2.

We can easily check this when a cache is disabled or in the controller, you can get this from customer session.

class Index extends \Magento\Framework\App\Action\Action
{
	/**
 	* @var  \Magento\Customer\Model\Session
 	*/
	public $customerSession;

	/**
 	* @param \Magento\Framework\App\Action\Context $context
 	* @param \Magento\Customer\Model\Session $customerSession
 	*/
	public function __construct(
    	\Magento\Framework\App\Action\Context $context,
    	\Magento\Customer\Model\Session $customerSession
	) {
    	$this->customerSession = $customerSession;
    	parent::__construct($context);
	}
	public function execute()
	{
    	if($this->customerSession->isLoggedIn()) {
        	// Customer is logged in

    	} else {
        	// Customer is not logged in
    	}

	}
}

Above code will work in the controller. This code will not work in block or helper when a cache is enabled.

Because When a cache is enabled, as soon as layout generation started, customer session will be cleared by \Magento\PageCache\Model\Layout\DepersonalizePlugin::afterGenerateXml on all cacheable pages. So we can’t get any customer session data from \Magento\Customer\Model\Session.

So, how to check customer is logged in or not when the page cache is enabled?

There is two way to achieving our goal
1. Using cacheable false in layout
2. Using httpContext

1. Using cacheable false in layout

<block class="Aureatelabs\ModuleName\Block\CustomBlock" name="aureatelabs_custom_block" template="Aureatelabs_ModuleName::custom.phtml" cacheable="false"/>

Using cacheable false for your custom block in layout, you will get all customer session data in your custom block now.

But there is one drawback of this method. When you use cacheable=false XML attribute in your layout entire page will disable Full Page Caching for the whole page, making the pages sourcing from that layout file extremely slow.

The most prevalent and harmful misinformation on full page caching is around the usage of the cacheable=false XML attribute. Including this attribute on a block will make any page that uses that block uncacheable. Unfortunately, there’s a lot of misinformation floating around and many cases where developers are using this to “solve” their problem, but actually breaking FPC.If you don’t want to disable your full page caching, there is another way using httpContext.

2. Using httpContext

Initialize \Magento\Framework\App\Http\Context in your Block or Helper. I have injected in my custom block.

protected $httpContext;

public function __construct(
	\Magento\Framework\View\Element\Template\Context $context,
	\Magento\Framework\App\Http\Context $httpContext,
	array $data = []
) {
	$this->httpContext = $httpContext;
	parent::__construct($context, $data);
}

public function getCustomerIsLoggedIn()
{
	return (bool)$this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
}

This method will return a boolean value. If the customer is logged in it will return true and when the customer is not logged in it will return false.

To get customer session data like customer id, name and email when a cache is enabled, please refer our next blog “How to get customer session data when a cache is enabled”.

Speak your Mind

Post a Comment

Got a question? Have a feedback? Please feel free to leave your ideas, opinions, and questions in the comments section of our post! ❤️

* This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Grow your online business like 4,117 subscribers

    * This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
    envelope

    Thank You!

    We are reviewing your submission, and will be in touch shortly.