How to stop login from customerLogin observer
-
16-10-2019 - |
Question
I would like to deny login for a customer group. Following an example I got a working observer but I am not able to stop the login process:
public function customerLogin($observer)
{
$customer = $observer->getEvent()->getCustomer();
$session = Mage::getSingleton('customer/session');
if ($customer->getData('group_id') == 4)
{
$session->setCustomer(Mage::getModel('customer/customer'))->setId(null);
Mage::throwException(__('This account is not activated.'));
}
}
Solution
This is pretty straight forward, you just need to unset the details stored against the customer/session.
public function customerLogin($observer)
{
$customer = $observer->getEvent()->getCustomer();
$session = Mage::getSingleton('customer/session');
if ($customer->getData('group_id') == 4)
{
$session->setId(null)
->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID)
->getCookie()->delete('customer');
Mage::throwException(__('This account is not activated.'));
return;
}
}
OTHER TIPS
I found something and now this works, but Luke's solution is better.
// redirect to new page
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('index'));
Mage::app()->getResponse()->sendResponse();
// Logout
Mage::getSingleton('customer/session')->logout();
exit;
Note, that it stops working if you logout before redirecting or if you throwException()
.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange