Tuesday 7 February 2012

Magento : How to redirect customer to login page if not logged in

If you are developing a module which needs to give access to its content only to logged in user then the preDispatch function will be very useful. This dispatches event before action.

Just write the following function in your module’s controller and customer log in is checked before each of your controller action.


/**
 * Checking if user is logged in or not
 * If not logged in then redirect to customer login
 */
public function preDispatch()
{
    parent::preDispatch();
  
    if (!Mage::getSingleton('customer/session')->authenticate($this)) {
        $this->setFlag('', 'no-dispatch', true);
    }
}

No comments:

Post a Comment