7

I am try add new action on AccountController

Now: AccountController is override properly

But it is whenever hit new Action (ajaxLoginPostAction) is redirect to 302.

I add ajaxLoginPost() as open action in preDispatch() function but till is not works.

Here config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
@author Amit Bera 
-->
<config>
    <modules>
        <Bluehorse_Ajaxlogin>
            <version>1.0.0</version>
        </Bluehorse_Ajaxlogin>
    </modules>
    <!-- rewrite Accont Controller -->
    <frontend>
        <routers>
            <customer>
                <args>
                    <modules>
                        <ajaxlogin before="Mage_Customer">Bluehorse_Ajaxlogin</ajaxlogin>
                    </modules>
                </args>
            </customer>
        </routers>
            <layout>
            <updates>
                <ajaxlogin>
                    <file>ajaxlogin.xml</file>
                </ajaxlogin>
            </updates>
        </layout>
      </frontend>
      <global>
          <blocks>
              <ajaxlogin>
                  <class>Bluehorse_Ajaxlogin_Block</class>
              </ajaxlogin>
          </blocks>
          <helpers>
              <ajaxlogin>
                  <class>Bluehorse_Ajaxlogin_Helper</class>
              </ajaxlogin>
          </helpers>
      </global>
</config>

AccountController.php

<?php
/* @ Purpose  ajax login
 * @ Author Amit Bera<[email protected]>
 * @ Module Bluehorse_Ajaxlogin
*/
require_once Mage::getModuleDir('controllers', 'Mage_Customer').DS.'AccountController.php';
class Bluehorse_Ajaxlogin_AccountController extends Mage_Customer_AccountController{

   /*   Add new Action 
    */
    protected $_cookieCheckActions = array('loginPost', 'createpost','ajaxLoginPost');
   protected $defaultOpenActionList=
        array(
            'create',
            'login',
            'logoutsuccess',
            'forgotpassword',
            'forgotpasswordpost',
            'resetpassword',
            'resetpasswordpost',
            'confirm',
            'confirmation',
           'loginPost', 
           'createpost'
        );

    protected  $newOpenActionList= array(
            'ajaxloginPost'

        );




    /* Check customer authentication for some actions */
    public function preDispatch() {

         $currenAction=$this->getRequest()->getActionName();

        $pattern = '/^(' . implode('|', $this->newOpenActionList) . ')/i';

        if (preg_match($pattern, $currenAction)):

            $TempAction=  $this->getRequest()->setActionName('index');
         endif;

         parent::preDispatch();

         if($currenAction!=$this->getRequest()->getActionName()){
            $this->getRequest()->setActionName($currenAction);
        }

        if(!$this->getRequest()->isDispatched()){
            return;
        }

        if (!preg_match('/^('.$this->_getValidActions().')/i', $currenAction)) {

             if (!$this->_getSession()->authenticate($this)) {
              $this->setFlag('', 'no-dispatch', true);
             }
        } else {

             $this->_getSession()->setNoReferer(true);
        }

     }
     protected function _getValidActions(){
      return implode("|", array_merge($this->defaultOpenActionList, $this->newOpenActionList));
      }
    public function ajaxLoginPostAction(){

        $result = array();

        if (!$this->_validateFormKey()) {
                $result['success'] = 0;
                $result['error'] = $this->_getHelper('customer')->__('Invalid form key.');
             Mage::throwException('Invalid form key');
            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()) {

                        $result=$this->_AjaxwelcomeCustomer($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->setUsername($login['username']);
                    $result['success'] = 0;
                    $result['error'] =$message;

                } catch (Exception $e) {
                    // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
                $result['success'] = 0;
                $result['error'] =$e->getMessage();

                }
            } else {
                $result['success'] = 0;
                $result['error'] =$this->__('Login and password are required.');
            }
        }
        $this->getResponse()->setHeader('Content-type', 'application/json');
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

        }


    protected function _AjaxwelcomeCustomer(Mage_Customer_Model_Customer $customer, $isJustConfirmed = false)
    {
        $result=array();

            $result['success'] = 1;
            $result['message'] = $this->__('Thank you for registering with %s.', Mage::app()->getStore()->getFrontendName());

        if ($this->_isVatValidationEnabled()) {
            // Show corresponding VAT message to customer
            $configAddressType =  $this->_getHelper('customer/address')->getTaxCalculationAddressType();
            $userPrompt = '';
            switch ($configAddressType) {
                case Mage_Customer_Model_Address_Abstract::TYPE_SHIPPING:
                    $userPrompt = $this->__('If you are a registered VAT customer, please click <a href="%s">here</a> to enter you shipping address for proper VAT calculation',
                        $this->_getUrl('customer/address/edit'));
                    break;
                default:
                    $userPrompt = $this->__('If you are a registered VAT customer, please click <a href="%s">here</a> to enter you billing address for proper VAT calculation',
                        $this->_getUrl('customer/address/edit'));
            }

            $result['success'] = 1;
            $result['message'] = $userPrompt;
        }

        $customer->sendNewAccountEmail(
            $isJustConfirmed ? 'confirmed' : 'registered',
            '',
            Mage::app()->getStore()->getId()
        );

        return $result;
    }

}

I cannot find it problem.

Can any one have solution

Update:

$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result))

redirect to customer/account/login using 302

2 Answers 2

2

You get redirected because you call parent::preDispatch() in your code.
This calls the initial method and it does not pass the validation because you action is not in the list of allowed actions

    $openActions = array(
        'create',
        'login',
        'logoutsuccess',
        'forgotpassword',
        'forgotpasswordpost',
        'resetpassword',
        'resetpasswordpost',
        'confirm',
        'confirmation'
    );

But why do you need to rewrite the default account controller? Can't you just have your own controller that does not depend on the functionality of the default one? You controller should only contain ajaxloginPostAction() that does what you need. I assume it should be something like the loginPostAction does but returns the response as json.
It should work in theory.

2
  • Thanks for advice .But i have change current action Name Dispatch whenever ajaxloginPostAction() is predispatch by if (preg_match($pattern, $currenAction)): $TempAction= $this->getRequest()->setActionName('login'); endif;
    – Amit Bera
    Commented Aug 5, 2015 at 10:00
  • for skip parent parent::preDispatch(); Thanks again
    – Amit Bera
    Commented Aug 5, 2015 at 10:01
2

Reset Header

I got this the solution by changing header response content type.

  • First clear current header by $this->getResponse()->clearHeaders()
  • Then set header response with content type -> application/json

So change doing from :

$this->getResponse()->setHeader('Content-type', 'application/json');

to

$this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);

And give 302 redirection with required result json data.

Follow:

Alan Storm answer: https://stackoverflow.com/a/4442879/2940291

& @philwinkle https://magento.stackexchange.com/a/16238

Got i got the idea from their

Still Show 302 redirection:

After add this it show the 302 redirection but it given json data as i want.

Still 302 header redirection with return proper.

Solution:

Now it remember that i have set indexAction as temp action for ajaxloginPostAction(). which may be create issue.

 $TempAction=  $this->getRequest()->setActionName('index');

And it is right.I have change it loginAction which is open action in

Mage_Customer_AccountController and my Override controller Bluehorse_Ajaxlogin_AccountController

Now change to

 $TempAction=  $this->getRequest()->setActionName('index');

to

 $TempAction=  $this->getRequest()->setActionName('login');


Now  No more 302 redirection.

Your Answer

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

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