Question

I am facing problem, I don't know how to use login functionality in my custom module controller without pasting whole code of login. I want to hit the login function through my controller and login function return the session/result.

Please any 1 can me out.

I have 1.4.2 version of magento.

Was it helpful?

Solution

What have you tried ? Have you took a look at the standard magento controller (Mage_Customer_AccountController::loginPostAction) ? it's not so much line of codes ..

$session = Mage::getSingleton('customer/session');
$session->login($username, $password);

and try...catch + message/error handling

OTHER TIPS

Please look app/code/core/Mage/Customer/controllers/AccountController.php and public function loginPostAction(), you will get clear idea how magento handles exception

$session = Mage::getSingleton('customer/session');
 try {
    $session->login($username, $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. <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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top