How to check if the customer is logged in or not in Magento 2?

Jun 12th, 2018 3 min to read

There are some functionalities that we want only the logged-in customers to have access to.  In order to achieve this, there must be some way to check if a customer is logged in or not.

There are two ways by which you can check if a customer is logged in or not:

1) By using Object Manager

It is always a bad practice to use ObjectManager directly.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
	$customerSession = $objectManager->get('Magento\Customer\Model\Session');
	if($customerSession->isLoggedIn()) {
	   // customer login action
	}

2) By Injecting Class (Dependency Injection)

As said earlier, it is one of the worst practices to use the ObjectManager directly. Therefore an alternate way to check if a user is logged in or not is by using the following code in any class except the template files:

You first need to inject the following class in your constructor: 
/Magento/Customer/Model/Session

protected $_session;

	public function __construct(
	    ...
	    \Magento\Customer\Model\Session $session,
	    ...
	) {
	    ...
	    $this->_session = $session;
	    ...
	}

Then you need to use Magento\Customer\Model\Session::isLoggedIn() to check if the customer is logged in or not.

if ($this->_session->isLoggedIn()) {
	    // Customer is logged in 
	} else {
	    // Customer is not logged in
	}

Thank you for reading this article. Your feedback is welcome in the comments section, and we’re available to assist with any Magento 2 store development requirements you may have.

FAQs

How to login customer programmatically in Magento 2?


To log in to the customer programmatically, retrieve the customer object using the customer ID or email using CustomerRepositoryInterface
$customer = $this->_customerRepository->getById($customerId);
                                                   Or
$customer = $this->_customerRepository->get($customerEmail);
Next, use the Magento\Backend\Model\Auth\Session class to set the customer’s session as the current session.
$this->_authSession->setCustomerAsLoggedIn($customer);
Further, redirect the customer to the desired page – $this->_redirect(‘customer/account’);

How can I get customer data using customer ID in Magento 2?


To get customer data by using the customer’s ID, follow the below-mentioned steps –
1. Create an instance of the Magento\Customer\Api\CustomerRepositoryInterface class.
2. Next, use the getById method to retrieve customer data by ID. This method takes one argument, the customer ID, and returns a customer interface object.
3. Further, you can access the customer data using the getter methods provided by the customer interface.

How do I find the customer list in Magento 2?


To find the customer list in Magento 2 use class, follow the below-mentioned steps 
1. Create an instance of the Magento\Customer\Model\ResourceModel\Customer\Collection class.
$customerCollection = $this->_customerCollectionFactory->create();
2. If required, add filters or sorting to the collection by using the following code.
$customerCollection->addFieldToFilter('email', ['like' => '%@example.com']);
$customerCollection->setOrder('created_at', 'DESC');

3. Next, load the collection by calling the load function.
$customerCollection->load();
4. Now, iterate over the collection to get the customer data
foreach ($customerCollection as $customer)
 {
  echo $customer->getId() . '-' . $customer->getName();
}

How can I activate customer account in Magento 2?


To activate a customer account in Magento 2, follow the below-mentioned steps –
1. Log in to the Admin Panel and go to Stores —> Settings —> Configuration.
2. Navigate the left panel & expand Customers.
3. Next, select Login as Customer.
4. Further from the dropdown list, select – 
Yes for Enable Extension 
No for Disable Page Cache For Admin User
Auto-Detection (default) for Set Store View to Log and tick mark Use System Value
5. Thereafter, click on Save Config.

Vishal Gelani
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 2,452 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.