0

I am writing after plugin on create account method, to change the API response with. I have to call

createCustomerAccessToken($username, $password)

function, which has two parameters. What I am doing is i take the username from createAccount method and pass it to createCustomerAccessToken function.I can easily get the $username value how i can get the password parameter value?

<?php

namespace Mlameh\Store\Plugin;

class AddAccessToken
{
/**
 * @var \Magento\Customer\Api\Data\CustomerExtensionFactory
 */
private $customerExtensionFactory;
/**
 * @var \Magento\Integration\Model\Oauth\TokenFactory
 */
private $tokenModelFactory;
/**
 * @var \Magento\Integration\Model\CustomerTokenService
 */
private $customerTokenService;
private $token;

public function __construct(
    \Magento\Integration\Model\CustomerTokenService $customerTokenService
) {
    $this->customerTokenService = $customerTokenService;
}

/**
 * Add Login token to customer
 *
 * @param \Magento\Customer\Api\AccountManagementInterface $subject ,
 * @param \Magento\Customer\Api\Data\CustomerInterface $customer
 * @return  \Magento\Customer\Api\Data\CustomerInterface
 * @throws \Magento\Framework\Exception\AuthenticationException
 */
public function afterCreateAccount(
    \Magento\Customer\Api\AccountManagementInterface $subject,
    \Magento\Customer\Api\Data\CustomerInterface $customer
) {
    
    //$password = 
    $token['token'] = $this->customerTokenService->createCustomerAccessToken(
        $customer->getEmail()
    );
    return $customer;
}
}

1 Answer 1

1

You can use the arguments of the observed method in your after plugin as well. As I can see you are observing following method which has three arguments

\Magento\Customer\Model\AccountManagement:createAccount(CustomerInterface $customer, $password = null, $redirectUrl = '')

You can simply use all the needed arguments of observed methods in your plugin as

public function afterCreateAccount(
    \Magento\Customer\Api\AccountManagementInterface $subject,
    \Magento\Customer\Api\Data\CustomerInterface $result,
    $customer,
    $password = null
) {
   /* Your code */
}

For more clarification refer https://devdocs.magento.com/guides/v2.4/extension-dev-guide/plugins.html

Hope it was helpful. Thanks

2
  • If i pass the $password =null, then it will passed same null value to createCustomerAccessToken method.right?
    – sudo55
    Aug 17, 2020 at 7:11
  • I need the password value which user enter in Login API parameter.
    – sudo55
    Aug 17, 2020 at 7:11

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.