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

In Magento 2 we can check Customer is logged in or not by below way,
Using Block file, Pass Magento\Framework\App\Http\Context as a dependency to construct method.

Call getLogin function in template file,

Using ObjectManager, Never use Objectmanager directly in real code in magento 2. Here I have show just demonstrate using Objectmanager. Using Objectmanager directly create performance issue for the site.


7 Replies to “How to check if customer is logged in or not in magento 2?”

  1. First of all thanks for this great post. Yes using object manager directly in the code is a bad practice. However, it will make your guide more worthy if you will show some example and the output of the code implementation.

  2. Hi Syed Muneeb Ul Hasan, You can get Customer session by below way also, But using below way, When you use varnish or any other third party Caching system, Sometimes In homepage you cant get current session by below best way to get check customer login or not using \Magento\Framework\App\Http\Context $httpContext way.
    public function __construct(
    \Magento\Customer\Model\Session $customerSessiony to
    ) {
    $this->session = $customerSession;

    check using,
    $isLoggedIn = $this->session->isLoggedIn()

Leave a Reply

Your email address will not be published. Required fields are marked *