Magento 2 Logout Customer Programmatically– Sometimes we need to logout customer programmatically based upon some condition or validation. Here in this article we are going to explain how you can logout customer from block, controller, model, observer or anywhere.
Magento 2 Logout Customer Programmatically Example
You can use object manager to logout customer session simply as below –
Magento 2 Logout Customer Programmatically Example:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManager->create('Magento\Customer\Model\Session'); if($customerSession->isLoggedIn()) { $customerSession->_logout(); echo "Logged Out Successfully!"; } |
The above example will logout the current customer session.