4

i am using magento 2.1.1 php version 7.0

this is my payload and response.

enter image description here

i want to add customer token in the response and i am using following magento 2 api :

http://host/index.php/rest/default/V1/customers

Example (*) in the response i want to add seesion id in following response :

{
"id": 2278,
"group_id": 1,
"created_at": "2018-11-13 14:06:24",
"updated_at": "2018-11-13 14:06:24",
"created_in": "Default Store View",
"email": "[email protected]",
"firstname": "test",
"lastname": "lastName",
"store_id": 1,
"website_id": 1,
"addresses": [],
"disable_auto_group_change": 0
"session_id": "b5la2wrgxx00oqu83yuvqdthnqjv1tmv"
}

Hope you guys understand my problem

5
  • Seeion id you mean customer token or PHP SESSION id ? Nov 14, 2018 at 5:59
  • yes @MohammadMujassam, I want to add customer token in response data. Nov 14, 2018 at 6:14
  • You can write plugin for this method to append the token Magento\Customer\Model\AccountManagement::createAccountWithPasswordHash(), And to generate the token refer this magento.stackexchange.com/questions/175480/… Nov 14, 2018 at 6:26
  • @MohammadMujassam, can you please tell me from which file we get response data? Nov 14, 2018 at 10:21
  • Magento\Customer\Model\AccountManagement this is model where buisness logic is there and Magento\Customr\Api\AccountManagementInterface this is interface. You can refer the Answer given by Ramkishan and accept the answer if that works. Nov 15, 2018 at 11:12

1 Answer 1

2

Create a afterCreateAccount plugin of AccountManagementInterface. You need to create a new customer attribute as well. Your plugin code will look like below.

<?php

namespace Vendor\Module\Plugin;

class AddAccessToken
{

    public function __construct(
        \Magento\Integration\Model\Oauth\TokenFactory $tokenModelFactory,
        \Magento\Customer\Api\Data\CustomerExtensionFactory $customerExtensionFactory
    ) {
        $this->tokenModelFactory = $tokenModelFactory;
        $this->customerExtensionFactory = $customerExtensionFactory
    }

    /**
     * Add token to customer object
     *
     * @param   \Magento\Customr\Api\AccountManagementInterface $subject,
     * @param   \Magento\Customer\Api\Data\CustomerInterface $customer
     * @return  \Magento\Customer\Api\Data\CustomerInterface
     */
    public function afterCreateAccount(
        \Magento\Customr\Api\AccountManagementInterface $subject,
         \Magento\Customer\Api\Data\CustomerInterface $customer
    ) {
        $token = $this->tokenModelFactory->create()->createCustomerToken($customer->getId());
        $extensionAttributes = $customer->getExtensionAttributes();
        if($extensionAttributes === null) {
                $extensionAttributes = $this->customerExtensionFactory->create();
       }
        $extensionAttributes->setToken($token);
        $customer->setExtensionAttributes($extensionAttributes);
        return $customer;
    }

}

di.xml

<type name="Magento\Customr\Api\AccountManagementInterface">
    <plugin name="add_access_token" type="Vendot\Module\Plugin\AddAccessToken"/>
</type>

extension_attributes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
    <attribute code="token" type="string" />
</extension_attributes>
</config>

Go into module.xml file and update setup_version to 1.0.1 and run setup:upgrade command. Let me know if any more help needed.

19
  • Did it work for you @Nilesh Nov 14, 2018 at 13:43
  • can you tell me how to add customer attribute and in which file? Nov 16, 2018 at 5:56
  • Please check updated answer. Nov 16, 2018 at 6:08
  • I have added eav attribute token as per your steps but getting following error : Fatal Error: 'Uncaught Error: Call to undefined method Magento\\Customer\\Model\\Data\\Customer::setToken() in /app/code/Vendor/Module/Plugin/AddAccessToken.php:26 Nov 16, 2018 at 6:37
  • What error, I hope you are aware about setup scripts. if you are not then I would like to let you know that InstallData only runs when your module is installed first time. If you want to run this for already installed module then run this script in UpgradeData.php Nov 16, 2018 at 6:40

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.