How to Check if a Customer is Logged into Magento 2
The Magento 2 stores offering impressive features do need to keep a check on them as it can be costly. The way out of this situation is to limit the features based on the customer groups. To implement it, the admin would need to check if a customer is logged into Magento 2 store. Only after confirming if the user is logged in, he/she can be offered the exclusive features.
Let’s say, you are using sign up popup in Magento 2. Sign up popup needs to be restricted the display for the logged in customers as it would be the most annoying thing you can do for your customers! You would need to check whether the user is logged into Magento 2 for doing so.
As the developers are going to need to know the login status of a user, I have posted the solution for the same.
Methods to Check if Customer is Logged into Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php namespace Vendor\Extension\Controller\Customer; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Customer\Model\Session; use Magento\Framework\App\Http\Context as AuthContext; class Index extends Action { private $customerSession; private $authContext; public function __construct(Context $context, Session $session, AuthContext $authContext) { $this->customerSession = $session; $this->authContext = $authContext; parent::__construct($context); } public function execute() { // by using Session model if ($this->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 } } } |
For Magento 2.3.x, use the below JS method to check if the customer is logged into Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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(); } }); }); |
Use any of the above methods to check if the user is logged in or not in Magento 2.
Similarly, you can check if a customer account is confirmed or not in Magento 2.
If you have any doubts in these methods, please mention them in the Comments section below and I’d be happy to help.
Feel free to share the post with fellow developers on social media.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Add a Custom Column in Order Grid in Magento 2
Meetanshi Magento Extensions Launches and Updates August [2019]
Next