How to Find out if a Customer is Logged into your Magento 2 or Not?

  • Posted by Envision Ecommerce
  • /
  • September 25, 2020

There may be several remarkable features that you are offering to your customers on your Magento 2 stores. And, there may be some situations arise where you want to put a limit on such features in tune with different customer groups. For instance, there are some exclusive features or functionalities that you only desire to offer to your logged-in customers. To facilitate its implementation, you would require checking if a customer is logged in or not into your Magento 2 store. Once after you verified about your customers’ log-in, you are able to offer them such exclusive features.

Let’s take a simple example here; you are displaying a sign-up popup on your Magento 2 store. This display of sign up popup should be limited to your logged in customers as it may be the most irritating things for them. To achieve it, you first need to confirm whether the user is logged in or not into your Magento 2 store.

To identify the login status of your customers and give them access to such exclusive features of your store based on their log-in activity, we have come up with this “how to” blog to help you:

Getting to verify if Customer is Logged into Magento 2:

customerSession = $session;
		$this->authContext = $authContext;
        parent::__construct($context);
    }

    public function execute()
    {
      // by using Session model
	  if($customerSession->isLoggedIn()) {
			// customer login code
	  }else{
	   // customer not login
	  }
	  
	  // using HTTP context
	  $isLoggedIn = $this->authContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
	  
	  if ($isLoggedIn) {
		//your coding
	  }
    }
}

Keep In Mind:

If it is a case where PageCache module is on (perhaps always on production), you should make sure that customer session is cleared on all cacheable pages by using the following command just after layout generation’s beginning,

\Magento\PageCache\Model\Layout\DepersonalizePlugin::afterGenerateXml

Now, if you verify the login status of your customers by means of HTTP context, it will still state ‘Yes’ but you will not be able to access customer data in your customer session to any further extent. Make sure to keep it as an important point to recheck earlier than attempting to access data in customer session. However, it is possible in a case of block, while is not likely in action controller as you are not anticipated to manually produce a layout there. Such layout will only be produced once after an instance of ‘ResultInterface’ is returned by the action controller.

If it is a case of Magento 2.3.x, you can implement the following JS method to validate if the user is logged into your Magento 2 store:

define([
    'uiComponent',
    'Magento_Customer/js/model/customer'
], function (
    Component,
    customer
) {
    'use strict';
 
    return Component.extend({
      
        /**
         * Check if customer is logged in
         *
         * @return {boolean}
         */
        isLoggedIn: function () {
            return customer.isLoggedIn();
        }
    });
});

That’s it! You can use any of the afore-mentioned solutions to find out if a customer is logged in or not. If you still have any doubt in your mind regarding this “How to” blog or just need a professional helping hand, our Magento Certified Developers are ready to implement these methods on your Magento 2 store on behalf of you. Feel free to send your queries at [email protected].