---
title: "Magento 2: How to Check if Customer is Logged in or Not?"
url: "https://magecomp.com/blog/magento-2-check-if-customer-is-logged-in-or-not/"
date: "2023-02-24T12:20:07+00:00"
modified: "2026-05-23T07:27:00+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 444
reading_time: "3 min read"
summary: "Hello Magento Friends,"
description: "If you want to know whether the customer is logged in or not to your Magento 2 store, check out these two methods."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Magento 2: Show Count 0 on Mini Cart when No Product is in Cart"
    url: "https://magecomp.com/blog/magento-2-show-count-0-on-mini-cart-when-no-product-is-in-cart/"
  - title: "How to Create Categories Tree Structure in Magento Programmatically?"
    url: "https://magecomp.com/blog/create-categories-tree-structure-magento-programmatically/"
  - title: "How to Add a Column to Tier Price in Magento 2 Admin?"
    url: "https://magecomp.com/blog/add-column-to-tier-price-magento-2-admin/"
---

# Magento 2: How to Check if Customer is Logged in or Not?

_Published: February 24, 2023_  
_Author: Dhiren Vasoya_  

![How to Check if Customer is Logged in or Not M2](https://magecomp.com/blog/wp-content/uploads/2023/02/How-to-Check-if-Customer-is-Logged-in-or-Not-M2-1024x512.webp)

**Hello Magento Friends,**

In today’s tutorial, I will show How to Check if a Customer is Logged in or not in Magento 2.
[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-1.webp)](https://magecomp.com/services/magento-upgrade-services/)

Sometimes store owners want to treat logged-in and non-logged-in customers differently. Say, for example, you want to offer discounts or promotions only to customers who are logged in to your store. Or you want to allow only logged-in customers to access some functionality of your store and restrict it for not logged-in customers. In such cases, you need the login status of customers.
You can [**force customers to log in**](https://magecomp.com/magento-2-required-force-login.html) to your store to take full advantage of the benefits you offer to your logged-in customers.
Increase the number of customer logins for your Magento 2 store by providing them with various quick login options like [**Social Login**](https://magecomp.com/magento-2-social-login-extension.html), [**Mobile Number Login**](https://magecomp.com/magento-2-mobile-otp-login.html), [**Google One Tap Login**](https://magecomp.com/magento-2-google-one-tap-login.html), and [**Ajax Popup Login**](https://magecomp.com/magento-2-ajax-login-register.html) to help them take maximum benefits from your store. To securely manage these diverse login methods at scale, integrating a dedicated [**Customer Identity and Access Management**](https://www.ory.com/customer-identity-and-access-management-ciam) platform is recommended.
Here, I have explained two ways to check whether the customer is logged in or not in Magento 2.
## Steps to Check if Customer is Logged in or Not in Magento 2:
We can check if the customer is logged in or not in Magento 2 using the below methods.
Let’s get started,
### Method 1: Using Helper file
Create a helper file at the below path.
**app/code/Vendor/Extension/Helper/Data.php**

Then add the code as follows
```
<?php

namespace VendorExtensionHelper;

class Data
{
    public function __construct(
       MagentoFrameworkAppHttpContext $httpContext
    ) {
        $this->httpContext = $httpContext;
    }

    public function getLogin() {
        return $this->httpContext->getValue(MagentoCustomerModelContext::CONTEXT_AUTH);
    }
?>
```

**Note –** Based on your requirement, you can use the getLogin() method in phtml, controller etc.
### [![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-1.webp)](https://magecomp.com/magento-performance-optimization.html)

### Method 2: Using Objectmanager
Create customer.php in your Magento root directory.
**{{magento_root}}/customer.php**

Now, add the following code
```
<?php

use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';

try
{
    $bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('MagentoFrameworkAppState');
    $appState->setAreaCode('frontend');

    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $userContext = $objectManager->get('MagentoFrameworkAppHttpContext');
    $loggedin = $userContext->getValue(MagentoCustomerModelContext::CONTEXT_AUTH);

    if($loggedin) {
      echo "Customer is Logged-In";
    }
}
catch(Exception $e)
{
    print_r($e->getMessage());
}
```

## Conclusion:
Hence, using the above methods, you can easily determine the customers that are logged in to your Magento 2 store. If you face any difficulty, share it with me through the comment section. Share the solution with your Magento friends and stay updated with us.
[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-1.webp)](https://magecomp.com/services/hyva-theme-development/)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/magento-2-check-if-customer-is-logged-in-or-not/](https://magecomp.com/blog/magento-2-check-if-customer-is-logged-in-or-not/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-13 01:02:06 UTC_  
