1

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;

    }
2
  • Cannot use ` die();` at here
    – Amit Bera
    Commented Jul 24, 2019 at 14:41
  • Ok. I tried, Then it is log out from external application. Not from magento application. Commented Jul 24, 2019 at 14:48

2 Answers 2

2

I am able to log out from two application from Magento and External application.

First I destroyed the Magento session and redirect to the external application from there I also deleted the session of external application. so it is logged out and I have written in such a way that after logout it redirected to Magento customer/account/logoutSuccess page. Done, Now Both applications are logout and redirect to Magento Homepage.

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 customerLogout
     * @package Vendor\MyModule\Observer
     */
    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;


    /**
     * MyObserver 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('http://example.com/logout.php');

             if($customerId) {
                //It will logout from Magento Application
                $this->customerSession->destroy(); 

                // And redirect to External application and logout
                $this->_responseFactory->create()->setRedirect($url)->sendResponse();

            } 

        }
    }

}
1

You have to clear from Magento cache Session variable mage-cache-sessid

<?php

namespace Mycompany\Customerhomepage\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\UrlInterface;
use Magento\Framework\App\ResponseFactory;
use Magento\Framework\App\ObjectManager;

/**
 * Class customerlogin
 * @package Mycompany\Customerhomepage\Observer
 */
class customerlogin implements ObserverInterface{
    protected $_responseFactory;
    protected $_url;
    /**
     * @var CookieMetadataFactory
     */
    private $cookieMetadataFactory;

    /**
     * @var PhpCookieManager
     */
    private $cookieMetadataManager;

    /**
     * @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');
    // run this condition when coming from external application
        if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
            $metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
            $metadata->setPath('/');
            $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
        }
        $this->_responseFactory->create()->setRedirect($customerBeforeAuthUrl)->sendResponse();

    }
    private function getCookieManager()
    {
        if (!$this->cookieMetadataManager) {
            $this->cookieMetadataManager = ObjectManager::getInstance()->get(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class);
        }
        return $this->cookieMetadataManager;
    }


    private function getCookieMetadataFactory()
    {
        if (!$this->cookieMetadataFactory) {
            $this->cookieMetadataFactory = ObjectManager::getInstance()->get(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class);
        }
        return $this->cookieMetadataFactory;
    }
}
4
  • It is showing blank page after redirecting to mymagento.com/customer/account/logout Commented Jul 25, 2019 at 5:55
  • Question is confuse me!. What you have trying to achieve. How you have to try to log out from external application and please explain the follow.
    – Amit Bera
    Commented Jul 25, 2019 at 7:44
  • Ok. I have an external application and Magento. I am redirecting to that site with observer class. I am able to login to both Magento and External application and redirect backs to Magento. when we log in to Magento, it goes to that application and reads the user data from Magento and log in to that site and redirect backs to Magento. Same way when we logout from Magento it goes to the external application read the properties of logout and redirect back to Magento. Commented Jul 25, 2019 at 9:25
  • Thanks @Amit Bera for helping Commented Jul 26, 2019 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.