3

How do I change the current optional password in edit account section in Magento 2 as customer is already logged in, hence I don't want customer to put the current password again for the store. How can I achieve this for my store?

0

You can achieve this my following steps:

Step 1: Copy the below file to your theme:

vendor/magento/module-customer/view/frontend/templates/form/edit.phtml

And remove required class from below section:

<div class="field password current">
    <label class="label" for="current-password"><span><?= $block->escapeHtml(__('Current Password')) ?></span></label>
    <div class="control">
        <input type="password" class="input-text" name="current_password" id="current-password" data-input="current-password" autocomplete="off" />
    </div>
</div>

Step 2: Copy the below file to your theme:

vendor/magento/module-customer/view/frontend/web/change-email-password.js

and change the method _showAll:

_showAll: function () {
    $(this.options.titleSelector).html(this.options.titleChangeEmailAndPassword);

    $(this.options.mainContainerSelector).show();
    $(this.options.emailContainerSelector).show();
    $(this.options.newPasswordContainerSelector).show();
    $(this.options.confirmPasswordContainerSelector).show();

    //$(this.options.currentPasswordSelector).attr('data-validate', '{required:true}').prop('disabled', false); // Comment this line
    $(this.options.currentPasswordSelector).prop('disabled', false); // Added this line
    $(this.options.emailSelector).attr('data-validate', '{required:true}').prop('disabled', false);
    this._updatePasswordFieldWithEmailValue();
    $(this.options.confirmPasswordSelector).attr(
        'data-validate',
        '{required:true, equalTo:"' + this.options.newPasswordSelector + '"}'
    ).prop('disabled', false);
},

Step 3: Override the following class

vendor/magento/module-customer/Model/AccountManagement.php

and change the function changePasswordForCustomer(Its private method if override not working then try to edit the vendor class)

private function changePasswordForCustomer($customer, $currentPassword, $newPassword)
{
    try {
        //$this->getAuthentication()->authenticate($customer->getId(), $currentPassword); // Comment this line
    } catch (InvalidEmailOrPasswordException $e) {
        throw new InvalidEmailOrPasswordException(__('The password doesn\'t match this account.'));
    }
    $customerEmail = $customer->getEmail();
    $this->credentialsValidator->checkPasswordDifferentFromEmail($customerEmail, $newPassword);
    $customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
    $customerSecure->setRpToken(null);
    $customerSecure->setRpTokenCreatedAt(null);
    $this->checkPasswordStrength($newPassword);
    $customerSecure->setPasswordHash($this->createPasswordHash($newPassword));
    $this->destroyCustomerSessions($customer->getId());
    $this->customerRepository->save($customer);

    return true;
}

Vendor edit is not good practice.

  • How to handle current password in AccountManagement model file...... – Mano M Jul 13 '18 at 18:35
  • Did you try to change the function I have mentioned? This function ignores the current password validation and checking. – Sukumar Gorai Jul 13 '18 at 18:38
  • Yes. i have done it. But i need to override account management in my module file ....How to solve this...I spend lot of time for this.. – Mano M Jul 13 '18 at 18:40
  • Without override everything works fine as you want? – Sukumar Gorai Jul 13 '18 at 18:42
  • Yes.....its working fine – Mano M Jul 13 '18 at 18:43

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.