Magento 2 stores with remarkable features should be under-eye because they are expensive. The solution to this problem is to limit functionalities depending on client groups. To execute this, the administrator must first determine whether a customer is logged into the Magento 2 store or not. Only after confirming that the user is logged in, it will be easy to offer them unique features

Because the developers will need to know about a user's login status, We have provided a solution for the same.

Basically, there are two ways to check if a customer is logged in or not!

1) By using Object Manager

It is said that using Object Manager Directly is a terrible idea. However let’s look at it first:

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

2) By Injecting a Class (Dependency Injection)

As stated previously, using the Object Manager directly is one of the worst methods. As a result, one alternative method for determining if a user is logged in or not is given, use the following code in any class other than the template files:

And for that, you’ll have to include the following class in your constructor: /Magento/Customer/Model/Session

protected $_session;

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

Later, you can check it easily if the customer is logged in or not by using Magento\Customer\Model\Session::isLoggedIn()

if ($this->_session->isLoggedIn()) {
    } else {

If you have any questions about these methods, please write them in the comments box below, and we'll be pleased to answer them.

And don't forget to share this article with fellow developers on social media.

Hire Adobe Certified Magento Developers Now