1

I have created a custom login module which loads a page to be used by a particular customer group with its own URL trade/account/login. However, I've noticed that if a user tries to login with an incorrect username and password the page redirects back to the default customer/account/login URL.

Is there a way I can keep the customer on the same page if they enter the wrong login details?

I would like to keep them on trade/account/login instead of redirecting to the customer account login customer/account/login

So far I only have this in my controller which loads the customer login page and form this works fine but doesn't keep the user on the same page if they enter the wrong username or password

<?php


class Namespace_Customlogin_AccountController extends Mage_Core_Controller_Front_Action{

    public function LoginAction() {

      $this->loadLayout();
      $this->renderLayout();

    }
}

I'm looking into the AccountController.php but I cannot see where the redirect happens so would like to ask if anyone knows where I can override it in my module please?

5
  • show your AccountController.php file Sep 19, 2014 at 11:59
  • @programmer_rkt Hi, please see my edit! Sep 19, 2014 at 12:19
  • what is action post url of your form ?
    – Amit Bera
    Sep 19, 2014 at 13:07
  • what to know this url not function
    – Amit Bera
    Sep 23, 2014 at 11:34
  • @AmitBera Apologies, trade/account/loginPost not sure if I am even doing this correctly at the moment... Sep 23, 2014 at 11:46

2 Answers 2

7

It was quite simple in the end

<?php if ($this->helper('customer')->isLoggedIn()): ?>
<?php Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account'));?>
<?php else: Mage::getSingleton('customer/session')->setBeforeAuthUrl(Mage::helper('core/url')->getCurrentUrl()); ?>
<?php endif ?>

If the custom successfully logs in then redirect to the customer account page otherwise stay on the current URL

1
  • Amazing answer buddy ! :) Sep 8, 2015 at 11:59
1

When a browser requests for a url like trade/account/login, magento will first look which module that uses trade as it's frontName. Now let us assume your custom module uses trade as it's front name.

Next parameter is account. Magento will use this to find corresponding controller file that has to process. So thus magento now looks for AccountController.php file in your module's controllers directory.

Next parameter is using to find the action. Here it is login. Hence Magento will look for a loginAction() in AccountController.php which resides in your module. (I can see you have used LoginAction(). It will not work for sure ).

When you submit a login credentials, it will post this data to another action that is defined in AccountController which is in your module. For an example, default AccountContoller in Mage_Customer uses logiPost action for this purpose. I assume in the similar way, your AccountController also holds a validation method. If it is not there, you need to create one. Let it be loginPost.

So in this case, first you need to refer your login form to this action. It will somewhat looks like this.

<form name="form_name" action="<?php echo $this->getUrl('trade/account/loginPost'); ?>">
      <!-- your form fields lives here -->
</form>

You have set url correctly.Now in your AccountController, you need to define loginPost action

File : app\code\<codePool>\Namespace\Modulename\controllers\AccountController.php

public function loginPostAction()
{
    $params = $this->getRequest()->getParams();
    //do your validation here
    if (<validation_fails>) {
          $this->_redirect('*/*/login'); // this will redirect to your login action
    }
}

As you can see redirectUrl() does the trick. It's parameter tells to magento, that we need to persist same current frontname and controller, but action should be login. Since current frontname and controllers are trade and account, it points towards your controller's login action

Use this data wisely and achieve your result. Happy debugging !

3
  • Hi, thanks for the feedback, there are a few things that don't seem to be correct though. At the moment my log form works with <?php echo $this->getPostActionUrl() ?> when I change it to <?php echo $this->getUrl('trade/account/loginPost'); ?> The form no longer works and redirects me to URL:trade/account/loginPost I'm not sure why but the syntax doesn't work.. Sep 19, 2014 at 13:09
  • 1
    I have taken loginPost() action as an example. I dont know whether it exist in your controller. This is not a full solution, rather gives you an insight what happends backside. Using this info dig more Sep 19, 2014 at 16:19
  • Below answer is more appealing and less complex, What do u think ?? Sep 8, 2015 at 11:59

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.