I am trying to logout from two applications
I have a logout observer, where I am trying to logout from external application and also from Magento.
Here Problem is
when I am logged out from the external application successfully, But not able to logout from Magento2 application. when I am logout from Magento2 application successfully, But not able to logout from external application
What I tried
\app\code\Mycompany\Customerhomepage\etc\frontend\events.xml
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/framework/Event/etc/events.xsd"> <event name="customer_logout"> <observer name="mycompany_mymodule_customer_logout" instance="Mycompany\MyModule\Observer\Customerlogout"/> </event> </config> \app\code\Mycompany\MyModule\Observer\Customerlogout.php
Here below code after setRedirect, I used die();. It will logout from external application successfully not from Magento2 application.
<?php namespace Mycompany\MyModule\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\UrlInterface; use Magento\Framework\App\ResponseFactory; /** * Class customerlogout * @package Mycompany\MyModule\Observer */ class Customerlogout implements ObserverInterface{ protected $_responseFactory; protected $_url; /** * @param UrlInterface $url * @param ResponseFactory $responseFactory */ public function __construct( \Magento\Framework\App\ResponseFactory $responseFactory, \Magento\Framework\UrlInterface $url ) { $this->_responseFactory = $responseFactory; $this->_url = $url; } /** * @param Observer $observer * @return void */ public function execute(Observer $observer) { $customerBeforeAuthUrl = $this->_url->getUrl('http://example.com/logout.php'); $this->_responseFactory->create()->setRedirect($customerBeforeAuthUrl)->sendResponse(); die(); } 2nd by changing die(); to $this. this logout from Magento2 not from external application
<?php namespace Mycompany\MyModule\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\UrlInterface; use Magento\Framework\App\ResponseFactory; /** * Class customerlogout * @package Mycompany\Customerhomepage\Observer */ class Customerlogout implements ObserverInterface{ protected $_responseFactory; protected $_url; /** * @param UrlInterface $url * @param ResponseFactory $responseFactory */ public function __construct( \Magento\Framework\App\ResponseFactory $responseFactory, \Magento\Framework\UrlInterface $url ) { $this->_responseFactory = $responseFactory; $this->_url = $url; } /** * @param Observer $observer * @return void */ public function execute(Observer $observer) { $customerBeforeAuthUrl = $this->_url->getUrl('http://example.com/logout.php'); $this->_responseFactory->create()->setRedirect($customerBeforeAuthUrl)->sendResponse(); $this; }