I am trying to get Magento to log me in through a form I built by using the _forward() method used to pass info to the proper controller. However, this is not working. I can't tell if the correct data is getting passed to the controller. By logging my variables, I can see that the data is there (username, hashed password, if the account actually exists, etc.). By the Magento documentation, I am calling the method properly, with the correct parameters.
Is there a step I am missing?
Here is my code and how it works:
function customerLogin()
{
jQuery(".bubblingG").show();
var email = jQuery('#login-email').val();
var password = jQuery('#login-password').val();
jQuery.post("orderkickoff/login/login", { 'login[username]': email, 'login[password]': password } );
window.location.replace("customer/account/");
}
This is jQuery on the front end that gets the email and the password and then posts that information to MY controller. By setting break points, I can see that my controller is getting called properly, and the data is there.
class Namespace_OrderKickoff_LoginController extends 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;
}
}
This is my controller that you can see gets the customers session, and then attempts to forward the data to the controller AccountController and call the loginPost function in order to log the user in.
This is where the process is failing. I step through everything and return to my function, but nothing happens. I have tried sending in an array with the username and password just like the documentation shows, but that still doesnt help. All that happens is I am redirected to the login page. I can't figure out why this is not logging me in and then redirecting me to the accounts page.
I also tried to use the login function, but this too fails. When the call to
if ($session->getCustomer()->getIsJustConfirmed())
is made, it returns null.
Here is the entire function
public function loginAction()
{
$login = $this->getRequest()->getPost('login');
$username = $login['username'];
$password = $login['password'];
$session = Mage::getSingleton('customer/session');
try {
$session->login($username, $password);
if ($session->getCustomer()->getIsJustConfirmed()) {
$this->_welcomeCustomer($session->getCustomer(), true);
$session->setBeforeAuthUrl(Mage::helper('customer')->getAccountUrl());
$session->setAfterAuthUrl(Mage::getUrl('customer/account/index'));
}
else{
Mage::log("There was an error");
}
} catch (Mage_Core_Exception $e) {
switch ($e->getCode()) {
case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
$value = Mage::helper('customer')->getEmailConfirmationUrl($login['username']);
$message = Mage::helper('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->addError($message);
$session->setUsername($login['username']);
} catch (Exception $e) {
Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
}
I have been stuck on this for days now.
Can anyone tell me what I am doing wrong?
Let me know if you need any more information.
Thanks!