Magento登录_forward方法失败

时间:2014-10-30 13:19:58

标签: php function magento login

我试图让Magento通过我使用_forward()方法构建的表单来登录我,该方法用于将信息传递给适当的控制器。但是,这不起作用。我无法判断正确的数据是否传递给控制器​​。通过记录我的变量,我可以看到数据存在(用户名,散列密码,如果帐户实际存在,等等)。通过Magento文档,我正确地使用正确的参数调用方法。

我缺少一个步骤吗?

以下是我的代码及其工作原理:

function customerLogin()
{
    var email    = jQuery('#login-email').val();
    var password = jQuery('#login-password').val();
    jQuery.post("orderkickoff/login/login", { 'login[username]': email, 'login[password]': password } );
}

这是前端的jQuery,它获取电子邮件和密码,然后将该信息发布到MY控制器。通过设置断点,我可以看到我的控制器被正确调用,数据就在那里。

类Namespace_OrderKickoff_LoginController扩展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;
}

}

这是我的控制器,您可以看到获取客户会话,然后尝试将数据转发到控制器AccountController并调用loginPost函数以便将用户登录。

这是流程失败的地方。我一步一步地回到我的功能,但没有任何反应。没有重定向,页面就在那里。我无法弄清楚为什么这不会登录我,然后重定向我。

谁能告诉我我做错了什么?

如果您需要更多信息,请与我们联系。

谢谢!

1 个答案:

答案 0 :(得分:1)

您需要更改登录逻辑。

public function loginAction(){
    if(!Mage::getSingleton('customer/session')->isLoggedIn())
    {
        $login = $this->getRequest()->getPost('login');
        $email = $login['username'];
        $customer = Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail($email);
        if($customer->getId())
        {
                $websiteId = Mage::app()->getWebsite()->getId();
            $store = Mage::app()->getStore();
            $customer = Mage::getModel("customer/customer");
            $customer->website_id = $websiteId;
            $customer->setStore($store);
            $customer->loadByEmail($email);
            Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
        }
        else
        {
            Mage::log("There is no customer with that email");
        }
    }

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