2

I am trying to get Magento to log me in through a form I built by using the _forward() method used to pass info to the proper controller. However, this is not working. I can't tell if the correct data is getting passed to the controller. By logging my variables, I can see that the data is there (username, hashed password, if the account actually exists, etc.). By the Magento documentation, I am calling the method properly, with the correct parameters.

Is there a step I am missing?

Here is my code and how it works:

function customerLogin()
{

    jQuery(".bubblingG").show();
    var email    = jQuery('#login-email').val();
    var password = jQuery('#login-password').val();
    jQuery.post("orderkickoff/login/login", { 'login[username]': email, 'login[password]': password } );       
    window.location.replace("customer/account/");


}

This is jQuery on the front end that gets the email and the password and then posts that information to MY controller. By setting break points, I can see that my controller is getting called properly, and the data is there.

class Namespace_OrderKickoff_LoginController extends Mage_Core_Controller_Front_Action
{

    public function loginAction()
    {
        //if customer is not logged in
        if(!Mage::getSingleton('customer/session')->isLoggedIn())
        {

            // get the email and load the customer by id
            $login = $this->getRequest()->getPost('login');
            $email = $login['username'];
            $customer = Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail($email);
            $quote = Mage::getSingleton('checkout/cart')->getQuote();

            //If the customer exists, log them in by forwarding to loginPost
            if($customer->getId())
            {
                // just make the customer log in
                $mysession = Mage::getSingleton('customer/session');
                $mysession->setBeforeAuthUrl(Mage::getUrl('customer/account/index'));
                $mysession->setAfterAuthUrl(Mage::getUrl('customer/account/index'));
                $this->_forward('loginPost','account','customer');
            }
            else
            {
                Mage::log("There is no customer with that email");
            }
        }

        $this->_redirect('customer/account/index');
        return;
    }
}

This is my controller that you can see gets the customers session, and then attempts to forward the data to the controller AccountController and call the loginPost function in order to log the user in.

This is where the process is failing. I step through everything and return to my function, but nothing happens. I have tried sending in an array with the username and password just like the documentation shows, but that still doesnt help. All that happens is I am redirected to the login page. I can't figure out why this is not logging me in and then redirecting me to the accounts page.

I also tried to use the login function, but this too fails. When the call to

if ($session->getCustomer()->getIsJustConfirmed())

is made, it returns null.

Here is the entire function

    public function loginAction()
    {
        $login = $this->getRequest()->getPost('login');
        $username = $login['username'];
        $password = $login['password'];

        $session = Mage::getSingleton('customer/session');
        try {
            $session->login($username, $password);
            if ($session->getCustomer()->getIsJustConfirmed()) {
                $this->_welcomeCustomer($session->getCustomer(), true);
                $session->setBeforeAuthUrl(Mage::helper('customer')->getAccountUrl());
                $session->setAfterAuthUrl(Mage::getUrl('customer/account/index'));
            }
            else{
                Mage::log("There was an error");
            }
        } catch (Mage_Core_Exception $e) {
            switch ($e->getCode()) {
                case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
                    $value = Mage::helper('customer')->getEmailConfirmationUrl($login['username']);
                    $message = Mage::helper('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
        }

I have been stuck on this for days now.

Can anyone tell me what I am doing wrong?

Let me know if you need any more information.

Thanks!

1 Answer 1

3

Any chance you are getting stuck because it is missing the form key?

Since you are working with 1.9 the first thing loginPost does is check the form key.

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

You can check if you have a form key by looking at your request.

$this->getRequest()->getParam('form_key', null)

And you can also check if the form key matches the current sessions form key, this can also be used to set your form key if you need to add it to your login form.

Mage::getSingleton('core/session')->getFormKey()
3
  • Thanks David! It looks like I was missing the form key. Does that have to be passed into the _forward function as well? I had noticed when looking at the XHR posts that Magento was sending in the username, password and this form key, but I was oblivious. Oct 30, 2014 at 14:14
  • Yes i guess you will need to forward that data also Oct 30, 2014 at 14:32
  • David I could kiss you! It was absolutely the form key! I had to add this line to my form <?php echo $this->getBlockHtml('formkey'); ?> And instead of posting from my jquery, I changed my div button to an actual submit button, and called loginPost directly. It takes over now and logs me in! thanks again! Oct 30, 2014 at 18:06

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.