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)

  1.   public function loginPostAction()
  2.   {
  3.   if ($this->_getSession()->isLoggedIn()) {
  4.   $this->_redirect('*/*/');
  5.   return;
  6.   }
  7.   $session = $this->_getSession();
  8.   if ($this->getRequest()->isPost()) {
  9.   $login = $this->getRequest()->getPost('login');
  10.   if (!empty($login['username']) && !empty($login['password'])) {
  11.   try {
  12.   $session->login($login['username'], $login['password']);
  13.   if ($session->getCustomer()->getIsJustConfirmed()) {
  14.   $this->_welcomeCustomer($session->getCustomer(), true);
  15.   }
  16.   } catch (Mage_Core_Exception $e) {
  17.   switch ($e->getCode()) {
  18.   case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
  19.   $value = Mage::helper('customer')->getEmailConfirmationUrl($login['username']);
  20.   $message = Mage::helper('customer')->__('This account is not confirmed. Click here to resend confirmation email.', $value);
  21.   break;
  22.   case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
  23.   $message = $e->getMessage();
  24.   break;
  25.   default:
  26.   $message = $e->getMessage();
  27.   }
  28.   $session->addError($message);
  29.   $session->setUsername($login['username']);
  30.   } catch (Exception $e) {
  31.   // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
  32.   }
  33.   } else {
  34.   $session->addError($this->__('Login and password are required.'));
  35.   }
  36.   }
  37.   $this->_loginPostRedirect();
  38.   }

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


  1.  $email="customer email address";
  2.  $password="customer password";
  3.  $session = Mage::getSingleton('customer/session');
  4.  $message=""; //error message to add to session
  5.  try {
  6.   $session->login( $email, $password );
  7.   if ($session->getCustomer()->getIsJustConfirmed()) {
  8.   //Customer is confirmed and successfully logged in
  9.   }
  10.  } catch (Mage_Core_Exception $e) {
  11.   //error occured
  12.   switch ($e->getCode()) {
  13.   case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
  14.   //email not confirmed actions here
  15.   break;
  16.   case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
  17.   //Email or password invalid actions here
  18.   $message = $e->getMessage();//echo the error message
  19.   break;
  20.   default:
  21.   $message = $e->getMessage(); //Display other error messages
  22.   }
  23.   $session->addError($message);
  24.  } catch (Exception $e) {
  25.   //login failed because of some other error.
  26.  }
  27.  

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.

    A

Leave a Reply

Your email address will not be published.

 

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