Usually, we can check whether a customer is logged in via the Customer Session (Magento\Customer\Model\Session) class. Sometimes when a full-page cache is enabled, the generic class will not work as per our expectation. So in that case we can use HttpContext (Magento\Framework\App\Http\Context) class to check whether a customer is logged in or not. HttpContext class defines customer_group and customer_not_logged_in by default.
Following is a small example of checking if a customer is logged in. The isCustomerLoggedId() function returns boolean value of customer logged in or not.
<?php
namespace <Namespace>\<ModuleName>\Block;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Customer\Model\Context;
class DemoClass {
/**
* @var HttpContext
*/
private HttpContext $httpContext;
/**
* Constructor
*
* @param HttpContext $httpContext
*/
public function __construct(
HttpContext $httpContext
) {
$this->httpContext = $httpContext;
}
/**
* Check if customer is logged
*
* @return bool
*/
public function isCustomerLoggedId()
{
return (bool)$this->httpContext->getValue(Context::CONTEXT_AUTH);
}
}
As we said HttpContext class defines only customer_group and customer_not_logged_in by default, if you want to get more details about the customer, you can create before plugin and set customer values in HttpContext class and get the same value in your desired place (block, helper, model, etc.) using same HttpContext class. For example, here we have Mage2 as a namespace and Demo as a module name.
Step 1: Define the plugin in di.xml
File path: Mage2/Demo/etc/frontend/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\App\FrontController">
<plugin name="defined_customerdata_to_context"
type="Mage2\Demo\Plugin\CustomerDataContext"/>
</type>
</config>
Step 2: Create a plugin class and implement a before plugin
File Path: Mage2/Demo/Plugin/CustomerDataContext.php
<?php
declare(strict_types=1);
namespace Mage2\Demo\Plugin;
use Magento\Framework\App\FrontController;
use Magento\Framework\App\RequestInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Http\Context;
class CustomerDataContext
{
/**
* @var Session
*/
private Session $customerSession;
/**
* @var Context
*/
private Context $httpContext;
/**
* Constructor
*
* @param Session $customerSession
* @param Context $httpContext
*/
public function __construct(
Session $customerSession,
Context $httpContext
) {
$this->customerSession = $customerSession;
$this->httpContext = $httpContext;
}
/**
* @param FrontController $subject
* @param RequestInterface $request
* @return RequestInterface[]
*/
public function beforeDispatch(FrontController $subject, RequestInterface $request)
{
$this->httpContext->setValue('customer_id', $this->customerSession->getCustomerId(), false);
$fullName = $this->customerSession->getFirstName() . " " . $this->customerSession->getLastName();
$this->httpContext->setValue('customer_fullname', $fullName, false);
return [$request];
}
}
Step 3: Get customer data in a model
File Path: Mage2/Demo/Model/Config.php
<?php
declare(strict_types=1);
namespace Mage2\PasswordProtection\Model;
use Magento\Framework\App\Http\Context as HttpContext;
class Config
{
/**
* @var HttpContext
*/
private HttpContext $httpContext;
/**
* @param HttpContext $httpContext
*/
public function __construct(
HttpContext $httpContext
) {
$this->httpContext = $httpContext;
}
/**
* @return array
*/
public function getLoggedInCustomer()
{
return [
'id' => $this->httpContext->getValue('customer_id'),
'fullname' => $this->httpContext->getValue('customer_fullname')
];
}
}
We hope this blog may understandable and useful to you. You can email us at mage2developer@gmail.com if we missed anything or want to add any suggestions. We will respond to you as soon as possible. Happy to help 🙂

