0

Can you suggest me how to redirect to forgot password page if the user inputs wrong email/password in the login page?

I tried it app/code/local/Mage/Customer/controllers/AccountController.php

public function loginPostAction()
{
    if (!$this->_validateFormKey()) {
        $this->_redirect('*/*/');
        return;
    }

    if ($this->_getSession()->isLoggedIn()) {
        $this->_redirect('*/*/');
        return;
    }
    $session = $this->_getSession();

    if ($this->getRequest()->isPost()) {
        $login = $this->getRequest()->getPost('login');
        if (!empty($login['username']) && !empty($login['password'])) {
            try {
                $session->login($login['username'], $login['password']);
                if ($session->getCustomer()->getIsJustConfirmed()) {
                    $this->_welcomeCustomer($session->getCustomer(), true);
                }
            } catch (Mage_Core_Exception $e) {
                switch ($e->getCode()) {
                    case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
                        $value = $this->_getHelper('customer')->getEmailConfirmationUrl($login['username']);
                        $message = $this->_getHelper('customer')->__('This account is not confirmed. <a href="%s">Click here</a> to resend confirmation email.', $value);
                        break;
                    case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
                        $message = $e->getMessage();
                        break;
                    default:
                        $message = $e->getMessage();
                }
                $session->addError($message);
                $session->setUsername($login['username']);
            } catch (Exception $e) {
                // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
            }
        } else {
            $session->addError($this->__('Login and password are required.'));
        }
    }

    $this->_loginPostRedirect();
}

I inserted the code to redirect: $this->_redirect('*/*/forgotpassword'); under case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:

public function loginPostAction()
{
    if (!$this->_validateFormKey()) {
        $this->_redirect('*/*/');
        return;
    }

    if ($this->_getSession()->isLoggedIn()) {
        $this->_redirect('*/*/');
        return;
    }
    $session = $this->_getSession();

    if ($this->getRequest()->isPost()) {
        $login = $this->getRequest()->getPost('login');
        if (!empty($login['username']) && !empty($login['password'])) {
            try {
                $session->login($login['username'], $login['password']);
                if ($session->getCustomer()->getIsJustConfirmed()) {
                    $this->_welcomeCustomer($session->getCustomer(), true);
                }
            } catch (Mage_Core_Exception $e) {
                switch ($e->getCode()) {
                    case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
                        $value = $this->_getHelper('customer')->getEmailConfirmationUrl($login['username']);
                        $message = $this->_getHelper('customer')->__('This account is not confirmed. <a href="%s">Click here</a> to resend confirmation email.', $value);
                        break;
                    case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
                        $message = $e->getMessage();
                        $this->_redirect('*/*/forgotpassword');
                        break;
                    default:
                        $message = $e->getMessage();
                }
                $session->addError($message);
                $session->setUsername($login['username']);
            } catch (Exception $e) {
                // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
            }
        } else {
            $session->addError($this->__('Login and password are required.'));
        }
    }

    $this->_loginPostRedirect();
}

But still not working. Any suggestions?

Ref: How can i Redirect customer to "Register" page if use email address and password is wrong?

Thanks

1 Answer 1

0

To redirect a customer on Forgot Your Password page, you can set BeforeAuthUrl for customer session under case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD as below.

public function loginPostAction()
{
    ...

    case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
        $message = $e->getMessage();
        $session->setBeforeAuthUrl($this->_getHelper('customer')->getForgotPasswordUrl());
        break;

    ...
}

I've tested it and I hope it will work in your case as well.

2
  • Perfect! Thank you!!
    – nhinzky
    Feb 3, 2017 at 9:36
  • happy to help you..
    – Shyam
    Feb 3, 2017 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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