How to login a customer/user programmatically in Magento

How to login a user programmatically in Magento? To find out how to do that, we can try to look at Magento code itself. so if we search for ->login in magento files, we will find among others. the following method inside Mage_Customer_AccountController (App/Code/Mage/Customer/controllers/AccountController.php)

public function loginPostAction() { 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()) { $this->_welcomeCustomer($session->getCustomer(), true); } } 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. Click here 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 } } else { $session->addError($this->__('Login and password are required.')); } } $this->_loginPostRedirect(); }

Based on that, we can take the useful part for us:

$email="customer email address"; $password="customer password"; $session = Mage::getSingleton('customer/session'); $message=""; //error message to add to session try { $session->login( $email, $password ); if ($session->getCustomer()->getIsJustConfirmed()) { //Customer is confirmed and successfully logged in } } catch (Mage_Core_Exception $e) { //error occured switch ($e->getCode()) { case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED: //email not confirmed actions here break; case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD: //Email or password invalid actions here $message = $e->getMessage();//echo the error message break; default: $message = $e->getMessage(); //Display other error messages } $session->addError($message); } catch (Exception $e) { //login failed because of some other error. }

2 thoughts on “How to login a customer/user programmatically in Magento

  1. Great stuff.

    I have done the same, reviewed whats in the core’s account controller. All works great.

    But what if I would like to login to a specific website/store?

    I have a couple of stores, and would like to log the user into a specific one.

    thanks for the post.


Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.