Magento2 Check If Customer Is Logged In Or Not

We can use the following code snippet to check whether the customer is logged in or not in Magento2 with the help of customer session model class object.


use Magento\Customer\Model\Session;

$protected customerSession;

/**
 * Class constructor.
 * @param Session $customerSession
 */
public function __construct(
    Session $customerSession    
)
{
    $this->customerSession = $customerSession;    
}

/**
 * check whether customer is logged in or not
 *
 * @return bool
 */
public function isCustomerLoggedIn()
{
    $this->customerSession->isLoggedIn() ? true : false;
}

Leave a Reply