1

I would like to extend the magento api so that a third party can, via web service or rest api, close the session of a client. Can you guide me on the steps that I should take? or some link with more information?

1
  • Even though @surath's answer is a complete module I don't think it will work. If I remember correctly the customer session is not available from the webapi_rest area. What data do you store about the user in the system that will be making the API call? Their email address or customer ID? It should be possible to invalidate all the users session tokens via the API but you would need to pass an identifier so Magento knows who's session to invalidate. – Craig Mar 15 at 23:10
0

Thanks a lot.

How should i run the test?

     curl -X GET https://URL_MAGENTO_SERVER/rest/V1/user/logout/   

But if I don't indicate the customer, how do you know which customer to log out of? Because I will call to logout from backend of other webapp...

I think that I've to pass the customerid or something similar ¿no?

0

Following code base you can do it easily.

vendorName/Module/API/CustomerManagementInterface.php

<?php
namespace VendorName\Module\Api;
interface CustomerManagementInterface
{

/**
 * Interface for get customer id
 *
 * @return int $customerId
 */

public function getCustomerId();


/**
 * Interface for get customer id
 *
 * @return int $customerId
 */
public function customerLogout();

}

vendorName/Module/Model/CustomerManagement.php

<?php

 namespace vendorName\Module\Model;

 class CustomerManagement implements \vendorName\Module\Api\CustomerManagementInterface
{

/**
 * @var \Magento\Customer\Model\Session
 */
protected $_customerSession;


public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Customer\Model\Session $customerSession
) {
    $this->_customerSession = $customerSession;
    $this->redirect = $context->getRedirect();
}

/**
 *
 * @return int $customerId
 *
 * @api
 *
 */
public function getCustomerId()
{

    if($this->_customerSession->isLoggedIn()){
        $customerId = $this->_customerSession->getId();
    }

    else{
        $customerId = 0;
    }
    return $customerId;

}


public function customerLogout()
{

   $customerId = $this->getCustomerId();
   
    if($customerId) {
        $this->_customerSession->logout()
            ->setBeforeAuthUrl($this->redirect->getRefererUrl())
            ->setLastCustomerId($customerId);
        return true;
    } else {
        return false;
    }
}
}

vendorName/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="vendorName\CustomerApi\Api\CustomerManagementInterface"
                type="vendorName\CustomerApi\Model\CustomerManagement" />
</config>

vendorName/Module/etc/webapi.xml

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/user/logout/" method="GET">
        <service class="vendorName\CustomerApi\Api\CustomerManagementInterface" method="customerLogout"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

vendorName/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="vendorName_Module" setup_version="1.0.0">
    </module>
</config>

vendorName/Module/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'vendorName_Module',
    __DIR__
);

Testing

{host}/V1/user/logout/ as a GET request

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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