Magento 2 How to Match Customer Password

Here, we will see how can we check if customer password is a match.
We can use the below code to check this.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerAccountManagement = $objectManager->get('Magento\Customer\Api\AccountManagementInterface');
try {
    $customer = $customerAccountManagement->authenticate($email, $password);
} catch (\Exception $e) {
    //Authentication Failed

We can use Magento\Customer\Api\AccountManagementInterface and call the authenticate method which requires two parameters. First parameter is email id of the customer and the second parameter is the password. It will return customer data model if it matches otherwise it will throw exception. So we have to write it in try-catch block.

Leave a Reply