cancel
Showing results for 
Search instead for 
Did you mean: 

How to check if a customer is logged in and display a block if they are not.

How to check if a customer is logged in and display a block if they are not.

I am trying to show a block that says "register for discounts" if a customer is not logged in, then when they register and log in, I want to hide that same block. I have tried to check for the "logged-in" class but it always returns true regardless of the fact that you are logged out. I have tried looking at solutions like this one but simply do not understand. I also tried looking at this solution but all i kept getting were errors. 

 

How can I leverage the isloggedin flag on any page?

5 REPLIES 5

Re: How to check if a customer is logged in and display a block if they are not.

Hello @robert_kregloh 

 

Please try the below code:

<?php
namespace [Vendor]\[Module]\Controller\Customer;

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($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
	  }
    }
}

Hope it helps.

Problem solved? Click Kudos and "Accept as Solution"

Re: How to check if a customer is logged in and display a block if they are not.

Thank you for the help, Could you add in an example of how I would call that in my cms block or phtml file? 

Re: How to check if a customer is logged in and display a block if they are not.

Once you enable Full page cache(FPC) in Magento, before rendering any cached page Magento removes customers properly from the customer session, this is called depersonalization

The responsibility to unset the private data lies in several depersonalization plugin classes.

 

These plugins are being applied to Magento\Customer\Model\Layout\DepersonalizePlugin

 

public function afterGenerateXml(\Magento\Framework\View\LayoutInterface $subject, $result)
{
if ($this->depersonalizeChecker->checkIfDepersonalize($subject)) {
$this->visitor->setSkipRequestLogging(true);
$this->visitor->unsetData();
$this->session->clearStorage();
$this->customerSession->clearStorage();
$this->session->setData(\Magento\Framework\Data\Form\FormKey::FORM_KEY, $this->formKey);
$this->customerSession->setCustomerGroupId($this->customerGroupId);
$this->customerSession->setCustomer($this->customerFactory->create()->setGroupId($this->customerGroupId));
}
return $result;
}

So injecting customer session in your class will not work, you will get null value once you enable FPC

 

The best approach of getting customer session will be following way 

  1. Use browser local storage
  2. Use KO JS to access local storage and assign to template
  3. Use sections.xml 

I have created a sample module for this you can refer the code 

https://github.com/skar2019/PrivateContent

Suman Kar(suman.jis@gmail.com) Magento Certified Developer Plus Skype: sumanphptech Problem solved? Please give 'Kudos' and accept 'Answer as Solution'.

Re: How to check if a customer is logged in and display a block if they are not.

Thank you for the code. Can you please guide me where I can add this code? Path location please!
Regards

Re: How to check if a customer is logged in and display a block if they are not.

Following code you can check customer login or not anywhere

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

Problem solved? Click Kudos and "Accept as Solution"