Magento2 – How to Check if Customer Password Matches

customermagento2password

I have a string variable, and i want to check if this string variable match with the current customer password, is there a way to do this?

Best Answer

Please check this model

Magento\Customer\Model\Authentication.php

In this You will find one method authenticate($customerId, $password)

    public function authenticate($customerId, $password)
    {
        $customerSecure = $this->customerRegistry->retrieveSecureData($customerId);
        $hash = $customerSecure->getPasswordHash();
        if (!$this->encryptor->validateHash($password, $hash)) {
            $this->processAuthenticationFailure($customerId);
            if ($this->isLocked($customerId)) {
                throw new UserLockedException(__('The account is locked.'));
            }
            throw new InvalidEmailOrPasswordException(__('Invalid login or password.'));
        }
        return true;
    }

You can call this method and pass $customerId, and Password string. If password will be correct then it will return true. Otherwise it will throw an exception.

Related Topic