How to logout from customer session by programming from observer in magento 2?
-
Yeah. Which event? – Robbie Averill Dec 31 '15 at 7:41
-
customer_register_success – SnVan Dec 31 '15 at 9:09
-
So you want people to register then log them out again straight away? Wouldn't it be easier to not log them in in the first place? – Robbie Averill Dec 31 '15 at 9:10
-
But I need to log them out through the observer! – SnVan Dec 31 '15 at 9:33
It's quite similar to the answer for the same question applied to Magento 1: you need to call the Customer session model's logout method. Just create a resultRedirect
and force headers after.
For an example of logging out, ref. \Magento\Customer\Controller\Account\Logout::execute()
:
public function execute()
{
$lastCustomerId = $this->session->getId();
$this->session->logout()->setBeforeAuthUrl($this->_redirect->getRefererUrl())
->setLastCustomerId($lastCustomerId);
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*/*/logoutSuccess');
return $resultRedirect;
}
You have to write this below code in your module's Observer folder.
Vendor/MyModule/Observer/CustomerLogoutObserver.php
<?php
namespace Vendor\MyModule\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\UrlInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Response\RedirectInterface;
class CustomerLogoutObserver implements ObserverInterface
{
/**
* @param \Magento\Framework\ObjectManagerInterface $objectManager
*/
protected $_objectManager;
/**
* @param \Magento\Framework\App\ResponseFactory $responseFactory
*/
protected $_responseFactory;
/**
* @param \Magento\Framework\UrlInterface $url
*/
protected $_url;
/*
* @param \Magento\Framework\App\ResponseInterface $response
*/
protected $_response;
/**
* @var \Magento\Framework\UrlInterface
*/
protected $_urlInterface;
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* CustomerLogoutObserver constructor.
* @param \Magento\Framework\ObjectManagerInterface $objectManager
* @param \Magento\Framework\App\ResponseFactory $responseFactory
* @param \Magento\Framework\UrlInterface $url
* @param \Magento\Framework\App\ResponseInterface $response
*/
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Framework\App\ResponseFactory $responseFactory
\Magento\Framework\App\ResponseInterface $response,
UrlInterface $urlInterface,
RedirectInterface $redirect,
\Psr\Log\LoggerInterface $logger,
\Magento\Customer\Model\Session $customerSession
) {
$this->_objectManager = $objectManager;
$this->_responseFactory = $responseFactory;
$this->_response = $response;
$this->_urlInterface=$urlInterface;
$this->_redirect=$redirect;
$this->logger = $logger;
$this->customerSession=$customerSession;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
try {
$customer = $observer->getEvent()->getData('customer');
$customerGroup = $customer->getGroupId();
$customerId= $this->customerSession->getCustomer()->getId();
$url = $this->_urlInterface->getUrl('customer/account/login');
if($customerId) {
$this->customerSession->destroy();
$this->_responseFactory->create()->setRedirect($url)->sendResponse();
}
}
}
}
then, Write an event for logout in events.xml in
Vendor/MyModule/etc/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="customer_logout">
<observer name="customer_logout_redirection"
instance="Vendor\MyModule\Observer\CustomerLogoutObserver" />
</event>
</config>
Try this, Its working for me.
public function execute() {
$lastCustomerId = $this->session->getId();
try {
$this->session->logout()->setBeforeAuthUrl($this->_redirect->getRefererUrl())->setLastCustomerId($lastCustomerId);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*/*/logoutSuccess');
return $resultRedirect;
}
if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
$metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
$metadata->setPath('/');
$this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
}
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*/*/logoutSuccess');
return $resultRedirect;
}