Password is a unique key that customers must use to access their accounts in your store, Changing password in Magento is not a big deal if you are logged in to the My Account area/Admin dashboard. I am talking about changing a customer’s password using a program. The below code will show you how to programmatically change a customer password without validating an old password.
Magento 2 new customer account is created via sign up, Also customer registration is possible via the admin panel. But, you can easily reset the password in your account.
Read This: How to Add Customer Attribute Programmatically in Magento 2
Programmatically change customer password create a PHP file in Magento root directory using following code. Do not forget to specify the customer id and password you want to set inside the code.
<?php use Magento\Framework\AppInterface; ini_set('display_errors', 1); try { require '../app/bootstrap.php'; } catch (\Exception $e) { echo $e->getMessage(); exit; } try{ $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $appState = $objectManager->get('\Magento\Framework\App\State'); $customerRepositoryInterface = $objectManager->get('\Magento\Customer\Api\CustomerRepositoryInterface'); $customerRegistry = $objectManager->get('\Magento\Customer\Model\CustomerRegistry'); $encryptor = $objectManager->get('\Magento\Framework\Encryption\EncryptorInterface'); $appState->setAreaCode('frontend'); $customerId = 11; // here assign your customer id $password = "Dolphin123"; // set your custom password $customer = $customerRepositoryInterface->getById($customerId); $customerSecure = $customerRegistry->retrieveSecureData($customerId); $customerSecure->setRpToken(null); $customerSecure->setRpTokenCreatedAt(null); $customerSecure->setPasswordHash($encryptor->getHash($password, true)); $customerRepositoryInterface->save($customer); echo 'Successfully Changes Your Password.'; } catch(\Exception $e){ print_r($e->getMessage()); }
customerRepositoryInterface is an instance of \Magento\Customer\Api\CustomerRepositoryInterface
customerRegistry is an instance of \Magento\Customer\Model\CustomerRegistry
encryptor is an instance of \Magento\Framework\Encryption\EncryptorInterface
Successfully executing this script your customer will be able to log in to their account using “Dolphin123” password.
This way we can update customer password in Magento2.
hope this instruction will be helpful for you.
Click one of our contacts below to chat on WhatsApp