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.

asked Jun 14, 2020 at 11:37

2 Answers 2

0

Following code is wrong:

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

Should be:

 $customer = $this->accountManagementInterface->authenticate($email, $password);
answered Jun 14, 2020 at 12:16
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;
 }
 .....
}
answered Jun 14, 2020 at 15:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.