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<amit.bera@bluehorse.in>
* @ 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
Best Answer
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
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 theloginPostAction
does but returns the response as json.It should work in theory.