Back to Top

Change Customer Password Programmatically in Magento2

Updated 28 February 2024

Change Customer Password Programmatically in Magento2:- In this blog I’ll explain you how we can change customer password programmatically.

$customerId = 1; // here assign your customer id
$password = "custom_password"; // set your custom password
$customer = $this->_customerRepositoryInterface->getById($customerId); // _customerRepositoryInterface is an instance of \Magento\Customer\Api\CustomerRepositoryInterface
$customerSecure = $this->_customerRegistry->retrieveSecureData($customerId); // _customerRegistry is an instance of \Magento\Customer\Model\CustomerRegistry
$customerSecure->setRpToken(null);
$customerSecure->setRpTokenCreatedAt(null);
$customerSecure->setPasswordHash($this->_encryptor->getHash($password, true)); // here _encryptor is an instance of \Magento\Framework\Encryption\EncryptorInterface
$this->_customerRepositoryInterface->save($customer);

In this way we can update customer password in Magento2.
Hope it will be helpful for you. 🙂

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home