0

I'm trying to see how to do this for the customer login process. After a customer enters the login credential, I'd like Magento to do the following. The login page is a custom page.

  1. If login works, Magento logs in the customer and directs the page (or wherever).

  2. If login fails, Magento does not redirect. Instead, it displays an error message on the current login page.

Note: I'm not familiar with Javascript, subsequently AJAX, and I just need high-level processing steps.

What are the proper procedures required for this? I think I need to make an AJAX request, but if I submit an AJAX request to log in, the returned response is just the HTML output string. So, that doesn't help to determine whether or not the login was successful. I don't want to encode a hidden string in the returned HTML and look for it in the HTML output.

Or, do I create a new controller action that the AJAX request that returns a page with a string (true or false, for example) that can subsequently be used to display error messages on the current login screen?

Or, do I need use some kind of customer login API that returns true or false, depending on the login result?

Let me know if I need to clarify.

1

Declare a new Module:

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Module>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Customer/>
            </depends>
        </Namespace_Module>
    </modules>
</config>

Create a Module:

app/code/local/Namespace/Module/

Create a config file:

app/code/local/Namespace/Module/etc/config.xml

Config:

<?xml version="1.0"?>
<config>
    <frontend>
        <routers>
            <namespace_login>
                <use>standard</use>
                <args>
                    <module>Namespace_Module</module>
                    <frontName>customlogin</frontName>
                </args>
            </namespace_login>
        </routers>
    </frontend>
</config>

Create a new controller:

app/code/local/Namespace/Module/controllers/IndexController.php

Controller:

<?php

class Namespace_Module_IndexController extends Mage_Core_Controller_Front_Action {


    public function indexAction() {

        if($this->getRequest()->isPost()) {
            $username = $this->getRequest()->getParam('username');
            $password = $this->getRequest()->getParam('password');
            $response = array();

            /** @var Mage_Customer_Model_Session $session */
            $session = Mage::getSingleton('customer/session');

            try {
                $session->login($username, $password);
                $response['redirect_url'] = 'your_url_here';
            } catch(Mage_Core_Exception $e) {
                $response['error'] = $e->getMessage();
            }

            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
        }

    }
}

Now, when you send your ajax request to: http://www.yourdomain.com/customlogin/, with the parameters username and password, it will attempt to login the user. It will then send the appropriate redirect URL to your AJAX response. If there is an error, it will send the error message back.

So, your JavaScript response should check for either response.error or response.redirect_url and parse appropriately.

I hope this helps!

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.