2014-10-30 23 views
0

我想讓Magento通過我使用_forward()方法構建的表單登錄,該方法用於將信息傳遞給適當的控制器。但是,這不起作用。我不知道是否正確的數據傳遞給控制器​​。通過記錄我的變量,我可以看到數據在那裏(用戶名,散列密碼,如果帳戶實際存在等)。通過Magento文檔,我使用正確的參數正確調用了方法。Magento登錄_forward方法失敗

有沒有我失蹤的一步?

這裏是我的代碼,它是如何工作的:

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的在前端,獲取電子郵件地址和密碼,然後職位信息對我的控制器。通過設置中斷點,我可以看到我的控制器正在調用正確,並且數據在那裏。

類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

你需要改變你的登錄邏輯。

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; 
} 
+0

謝謝。我認爲問題是我缺少form_key。這仍然不會通過我的自定義表單登錄。我不知道loadbyemail函數是否需要這個?我使用的是magento 1.9 – Pete 2014-10-30 16:26:14