Magento 2.3 – Fix Customer Reset Password Not Working

forgot-passwordmagento2

I am trying to reset my password as a customer. After going to the link received from email when i try to reset my password i am getting this error. See https://prnt.sc/p7ksmr.

Can any one help me in this.

namespace Smartwave\Porto\Model;


class AccountManagement extends \Magento\Customer\Model\AccountManagement
{

private function matchCustomerByRpToken(string $rpToken):CustomerInterface
{
    $this->searchCriteriaBuilder->addFilter(
        'rp_token',
        $rpToken
    );
    $this->searchCriteriaBuilder->setPageSize(1);
    $found = $this->customerRepository->getList(
        $this->searchCriteriaBuilder->create()
    );
    if ($found->getTotalCount() > 1) {
        //Failed to generated unique RP token
        throw new ExpiredException(
            new Phrase('Reset password token expired.')
        );
    }
    if ($found->getTotalCount() === 0) {
        //Customer with such token not found.
        throw NoSuchEntityException::singleField(
            'rp_token',
            $rpToken
        );
    }
    //Unique customer found.
    return $found->getItems()[0];
}

public function resetPassword($email, $resetToken, $newPassword)
{
    if (!$email) {
        $customer = $this->matchCustomerByRpToken($resetToken);
        $email = $customer->getEmail();
    } else {
        $customer = $this->customerRepository->get($email);
    }
    //Validate Token and new password strength
    $this->validateResetPasswordToken($customer->getId(), $resetToken);
    $this->credentialsValidator->checkPasswordDifferentFromEmail(
        $email,
        $newPassword
    );
    $this->checkPasswordStrength($newPassword);
    //Update secure data
    $customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
    $customerSecure->setRpToken(null);
    $customerSecure->setRpTokenCreatedAt(null);
    $customerSecure->setPasswordHash($this->createPasswordHash($newPassword));
    /*$this->sessionManager->destroy();
    $this->destroyCustomerSessions($customer->getId());
    $this->customerRepository->save($customer); */
$this->destroyCustomerSessions($customer->getId());
$this->sessionManager->destroy();
$this->customerRepository->save($customer);

    return true;
}

}

Best Answer

You need to override the /vendor/magento/module-customer/Model/AccountManagement.php in your theme.

etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Model\AccountManagement"
                type="Vendor\Module\Model\Rewrite\AccountManagement" />
</config>

We need to copy the full AccountManagement.php file as it contains private functions and variables and do the required changes in resetPassword().

Create AccountManagement.php file under Vendor\Module\Model\Rewrite with your code.

Hope it helps!!!

Related Topic