1

I am new to Magento. Please look at my steps. I need to redirect the customer to the dashboard, after a successful password reset.

  1. Click on the reset password link, and add customer email address.
  2. Customer will receive the password reset link.
  3. Once clicking the link, it will redirect to the password reset form.
  4. Once change the password,

"CUSTOMER SHOULD BE ABLE TO REDIRECT TO CUSTOMER DASHBOARD ITSELF. DON`T NEED TO LOGIN AGAIN."

Thanks

0

Here is the basic idea --

You need to override controller using <preference> in your module.

vendor/magento/module-customer/Controller/AccountResetPasswordPost.php

in this file you need to put below code in execute function-

public function execute()
    {
        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        $resetPasswordToken = (string)$this->getRequest()->getQuery('token');
        $customerId = (int)$this->getRequest()->getQuery('id');
        $password = (string)$this->getRequest()->getPost('password');
        $passwordConfirmation = (string)$this->getRequest()->getPost('password_confirmation');

        if ($password !== $passwordConfirmation) {
            $this->messageManager->addError(__("New Password and Confirm New Password values didn't match."));
            $resultRedirect->setPath('*/*/createPassword', ['id' => $customerId, 'token' => $resetPasswordToken]);
            return $resultRedirect;
        }
        if (iconv_strlen($password) <= 0) {
            $this->messageManager->addError(__('Please enter a new password.'));
            $resultRedirect->setPath('*/*/createPassword', ['id' => $customerId, 'token' => $resetPasswordToken]);
            return $resultRedirect;
        }

        try {
            $customerEmail = $this->customerRepository->getById($customerId)->getEmail();
            $this->credentialsValidator->checkPasswordDifferentFromEmail($customerEmail, $password);
            $this->accountManagement->resetPassword($customerEmail, $resetPasswordToken, $password);
            $this->session->unsRpToken();
            $this->session->unsRpCustomerId();
            $this->messageManager->addSuccess(__('You updated your password.'));

            /* start code to customer login and redirect to dashboard */
            // use dependency injection instead of object manager
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $customer = $objectManager->create('Magento\Customer\Model\Customer')->loadByEmail($customerEmail);

            $this->session->setCustomerAsLoggedIn($customer);
            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setUrl('url_to_customer_dashboard');
            return $resultRedirect;

            /* end code*/

            $resultRedirect->setPath('*/*/login');
            return $resultRedirect;
        } catch (InputException $e) {
            $this->messageManager->addError($e->getMessage());
            foreach ($e->getErrors() as $error) {
                $this->messageManager->addError($error->getMessage());
            }
        } catch (\Exception $exception) {
            $this->messageManager->addError(__('Something went wrong while saving the new password.'.$exception->getMessage()));
        }
        $resultRedirect->setPath('*/*/createPassword', ['id' => $customerId, 'token' => $resetPasswordToken]);
        return $resultRedirect;
    }

Extra Notes:-

(a) use dependency injection instead of object manager.

(b) use this link to know how to use preference.

I hope it would help.

  • @Wad, please mark it as answer it helped you. thanks – Shashank Kumrawat Nov 2 '18 at 6:52

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.