Perform Some Action after Customer successful Login : Magento

2.8k views Asked by At

I am creating a module in which I want to check some condition after customer successfully login, if condition is true then customer login otherwise not.

I know two ways of doing this :

  1. Overriding AccountController
  2. With Magento event.

My query are:

  1. which is the best way?
  2. Is there any event with which I can full fill my requirement?

Or if there is other best way of doing this, please recommend.

2

There are 2 answers

0
Manashvi Birla On BEST ANSWER

You need to use customer_login On the Mage_Customer_Model_Session model's method setCustomerAsLoggedIn() the event customer_login is dispatched.

config.xml

<customer_login>
    <observers>
        <yourobservername>
            <type>model</type>
            <class>yourmodule/path_to_class</class>
            <method>customerLogin</method>
        </yourobservername>
    </observers>
</customer_login>

and your Observer

class YourCompany_YourModule_Model_Observer
{
    public function customerLogin($observer)
    {
        $customer = $observer->getCustomer();
    }
}

Whenever a user is sucessfully logged in, the event customer_login will be fired and you have observed the method customerLogin() on that event, so your method from the observer will execute whenever a customer is successfully logged in. Here you can check your conditions as per requirements.

7
Navin Bista On

I think the best way to use magento event if possible. But in your case you have to check the condition before customer logins am i right? If so i don't think there is any events for that.So the best way is to override the controller.