First you create a custom module and then define a plugin by putting below code in app\code\VendorName\ModuleName\etc\di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Magento\Integration\Model\CustomerTokenService">
<plugin name="VendorName_ModuleName_Plugin" type="VendorName\ModuleName\Plugin\ModifyToken" sortOrder="10" disabled="false" />
</type>
</config>
After that create the file ModifyToken.php in VendorName\ModuleName\Plugin\ModifyToken directory and put the below code:
<?php
namespace VendorName\ModuleName\Plugin;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Integration\Model\Oauth\Token\RequestThrottler;
use Magento\Integration\Model\CredentialsValidator;
class ModifyToken
{
/**
* Customer Account Service
*
* @var AccountManagementInterface
*/
private $accountManagement;
/**
* @var \Magento\Integration\Model\CredentialsValidator
*/
private $validatorHelper;
/**
* @var RequestThrottler
*/
private $requestThrottler;
/**
* Initialize service
*
* @param AccountManagementInterface $accountManagement
* @param \Magento\Integration\Model\CredentialsValidator $validatorHelper
*/
public function __construct(
AccountManagementInterface $accountManagement,
CredentialsValidator $validatorHelper
) {
$this->accountManagement = $accountManagement;
$this->validatorHelper = $validatorHelper;
}
public function aroundCreateCustomerAccessToken(\Magento\Integration\Model\CustomerTokenService $subject, callable $proceed,$username, $password)
{
$this->validatorHelper->validate($username, $password);
$this->getRequestThrottler()->throttle($username, RequestThrottler::USER_TYPE_CUSTOMER);
try {
$customerDataObject = $this->accountManagement->authenticate($username, $password);
} catch (\Exception $e) {
$this->getRequestThrottler()->logAuthenticationFailure($username, RequestThrottler::USER_TYPE_CUSTOMER);
$modifiedresult=array();
$modifiedresult['token']=null;
$modifiedresult['status']=404;
$modifiedresult['success']=false;
$modifiedresult['message']="Please check credentials";
echo json_encode($modifiedresult);
exit;
}
$result = $proceed($username, $password);
return $result;
}
public function afterCreateCustomerAccessToken(\Magento\Integration\Model\CustomerTokenService $subject, $result)
{
$modifiedresult=array();
if($result){
$modifiedresult['token']=$result;
$modifiedresult['status']=200;
$modifiedresult['success']=true;
$modifiedresult['message']="Token has been successfully generated";
}
echo json_encode($modifiedresult);
exit;
}
/**
* Get request throttler instance
*
* @return RequestThrottler
* @deprecated 100.0.4
*/
private function getRequestThrottler()
{
if (!$this->requestThrottler instanceof RequestThrottler) {
return \Magento\Framework\App\ObjectManager::getInstance()->get(RequestThrottler::class);
}
return $this->requestThrottler;
}
}
public function aroundCreateCustomerAccessToken(\Magento\Integration\Model\CustomerTokenService $subject, callable $proceed,$username, $password, $devicetoken)– SagarPPanchal Feb 11 '19 at 06:55