How to Match Customer Password in Magento 2
Password is one of the most important customer login credentials that is used to authenticate the customer login. By default, Magento facilitates customer login by matching customer passwords with the database. However, sometimes you may also require to match customer passwords programmatically in Magento 2.
Suppose, you are developing a new login feature in your online Magento store. In such a case, you may need to match customer password in Magento 2 to authenticate customer login. Also if you want your customers to login to the Magento 2 store using only the Email ID then I would recommend you to Login Customer Programmatically Without Password in Magento 2.
Also, there can be instances where you want to reverify the customer password. For example, if you want to reverify the customers before allowing them to make any major changes in their accounts, you may require to match Magento 2 customer password.
Here, is the complete solution on how you can programmatically match customer password in Magento 2.
How to Match Customer Password in Magento 2
You can authenticate the customer password in Magento 2 by calling AccountManagementInterface and using the authenticate function. You can use the code provided below to match customer password in Magento 2 and authenticate the same for login.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
use Magento\Customer\Api\AccountManagementInterface; class Add extends Action { protected $accountManagementInterface; public function __construct( AccountManagementInterface $accountManagementInterface ) { $this->accountManagementInterface = $accountManagementInterface; parent::__construct($context); } public function execute() { $this->accountManagementInterface->authenticate($email, $password); } } $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $accountManagementInterface= $objectManager->get('Magento\Customer\Api\AccountManagementInterface'); try { $customer = $accountManagementInterface->authenticate($email, $password); } catch (\Exception $e) { //Incorrect email or password } |
The code authenticates the login if the customer password is a match and throws an exception in the case of an incorrect password. That’s it.
Also read: Magento 2 API – Get Customer Token
In case you have any query or doubt regarding the solution, feel free to comment it down below. I would be happy to help you.
Also, don’t forget to share this solution with your Magento friends via social media.
Thanks for reading.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Subscription Based Business Models in eCommerce [Experts’ Opinion]
How to Programmatically Get Root Category ID in Magento 2
Next