0

Hi I have created Rest API for customer login with custom module for my Magento 2.3 store.

I am entering parameters email and password for customer login. But it throws InvalidEmailOrPasswordException while firing api request.

Below is my code:

<?php
use Magento\Customer\Api\AccountManagementInterface;

class CustomerManagement
{

    /**
     * @var Magento\Customer\Api\AccountManagementInterface
     */
    protected $accountManagementInterface;
    protected $customer;
    protected $_customerSession;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        AccountManagementInterface $accountManagementInterface,
        \Magento\Customer\Model\Customer $customer,
        \Magento\Customer\Model\Session $customerSession
    ) {

        $this->objectManager = $context->getObjectManager();
        $this->resultPageFactory = $resultPageFactory;
        $this->accountManagementInterface = $accountManagementInterface;
        $this->customer = $customer;
        $this->_customerSession = $customerSession;
        parent::__construct($context);
    }


public function getLogin()
{
    $email = $this->request->getParam('email');
    $password = $this->request->getParam('password');

    try {
        /* @var $customer \Magento\Customer\Api\Data\CustomerInterface */
        $customer = $this->accountManagementInterface->authenticate($email, $password);
        $websiteId = $customer->getWebsiteId();

        /* @var $_customer \Magento\Customer\Model\Customer */
        $_customer = $this->customer->setWebsiteId($websiteId);

        $this->_customerSession->setCustomerAsLoggedIn($_customer->loadByEmail($email));   

    } catch(\Magento\Framework\Exception\InvalidEmailOrPasswordException $ex) {
        $errorData[0]['code'] = 7;
        $errorData[0]['message'] = 'Invalid Params!';
        $errorData[0]['exception'] = $ex;
        return $errorData;
    }
    .....
}

Here my code throws catch exception even if I use valid email and password for customer login API.

0

Following code is wrong:

    $customer = $this->accountManagementInterface->authenticate($newLoginValue, $password);

Should be:

    $customer = $this->accountManagementInterface->authenticate($email, $password);
| improve this answer | |
0

Email ID is missing. Update function as:

public function getLogin()
{
    $email = $this->request->getParam('email');
    $password = $this->request->getParam('password');

    try {
        /* @var $customer \Magento\Customer\Api\Data\CustomerInterface */
        $customer = $this->accountManagementInterface->authenticate($email, $password);
        $websiteId = $customer->getWebsiteId();

        /* @var $_customer \Magento\Customer\Model\Customer */
        $_customer = $this->customer->setWebsiteId($websiteId);

        $this->_customerSession->setCustomerAsLoggedIn($_customer->loadByEmail($email));   

    } catch(\Magento\Framework\Exception\InvalidEmailOrPasswordException $ex) {
        $errorData[0]['code'] = 7;
        $errorData[0]['message'] = 'Invalid Params!';
        $errorData[0]['exception'] = $ex;
        return $errorData;
    }
    .....
}
| improve this answer | |

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.