All about Magento E-commerce Store.......MagentoForum: October 2011

Tuesday, October 11, 2011

Magento: 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() {  
if (!Mage::getSingleton('customer/session')->authenticate($this)) {  
$this->setFlag('', 'no-dispatch', true);  } } 
Hope this helps. Thanks.