2

I'm using an observer that triggers every time customer edit it's account. I'm using customer_address_save_before for some reason this event also triggers when I modify my email, name, or password from front end customer. How can I verify if the customer edit it's password? something like $passwordOld = $customer->getOldPassword(); $passwordNew = $customer->getNewPassword();

Of course this are just examples, the methods does not exist. Thanks!

1

You can check/compare customer's old password and updated password value by below code.

<?php

namespace Vendor\Magento\Observer;

use Magento\Framework\Event\ObserverInterface;

class CustomerLogin implements ObserverInterface
{
    protected $encryptor;
    protected $_messageManager;
    protected $objectManager;

    public function __construct(
        \Magento\Framework\Encryption\EncryptorInterface $encryptor,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\ObjectManagerInterface $objectManager
    )
    {
        $this->encryptor = $encryptor;
        $this->_messageManager = $messageManager;
        $this->objectManager = $objectManager;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customer = $observer->getEvent()->getCustomer();
        $currentPasswordHash = $this->getCurrentPasswordHash($customer->getEntityId());
        try{
            $newPasswordHash = $this->encryptor->encrypt("new password value");
            if($currentPasswordHash == $newPasswordHash){
                // password is same
                $this->_messageManager->addWarning(__("New Password is same as current password. Please choose another one"));
            }
        }catch(\Exception $e){
            echo 'Error::'.$e->getMessage();
        }
    }

    private function getCurrentPasswordHash($customerId){
        $resource = $this->objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $sql = "Select password_hash from customer_entity WHERE entity_id = ".$customerId;
        $hash = $connection->fetchOne($sql);
        return $hash;
    }
}

Also, if you want to use decrypt password values to compare and check, you can use below line of code in your observer.

$decrypt = $this->encryptor->decrypt("password hash");

Hope it helps!!!

  • Hello, what value should I insert in $newPasswordHash = $this->encryptor->encrypt("new password value"); ? I cannot put manually the value of the password, because Customer will change it from front-end – Oscar Vazquez Sep 17 at 16:06
  • You can get it from the customer observer event. – Sumit Sep 17 at 16:07
  • But how? what is the method from $customer that gives me the password or new hash? – Oscar Vazquez Sep 17 at 16:08
  • Get post data from the request and pass the password value from the post data to the new password value. – Sumit Sep 17 at 16:10
  • your code is working, the only thing that I need to figure out is how to get the new password, I'm checking how to get the password from Create Account form. – Oscar Vazquez Sep 17 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.