1

I am using below url to generate token for customers

http://example.com/rest/V1/integration/customer/token

by passing username and password in json format

For Success response:

{
   "token": "u1ruwadnq3l3x1f00gwl3ivjkl5ssmrc",
   "status": 200,
   "success": true,
   "message": "Token has been successfully generated"
}

For failure response:

{
   "token": "null",
   "status": 401,
   "success": false,
   "message": "Please check credentials"
}

I have google a lot but i didnt find apt solution to satisfy my requirement as i am working on magento since one week

Aditya Shah
  • 7,617
  • 3
  • 39
  • 77
ramsai
  • 13
  • 5

1 Answers1

1

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;
}

}
Prasanta Hatui
  • 1,811
  • 1
  • 10
  • 16
  • It is working fine for success but for failure it is not working and it is giving default message { "message": "You did not sign in correctly or your account is temporarily disabled." } – ramsai May 23 '18 at 05:07
  • I have modified the result , please check – Prasanta Hatui May 23 '18 at 12:09
  • getting same message as i said above – ramsai May 23 '18 at 14:42
  • please run di:compile – Prasanta Hatui May 23 '18 at 15:39
  • @PrasantaHatui perfect answer +1 :)! can you please do me a favour, how can we add custom or another parameter after $password, ex. public function aroundCreateCustomerAccessToken(\Magento\Integration\Model\CustomerTokenService $subject, callable $proceed,$username, $password, $devicetoken) – SagarPPanchal Feb 11 '19 at 06:55