Magento redirect to login page while user not login on landing

Gowri picture Gowri · Jan 7, 2012 · Viewed 23.1k times · Source

I want to redirect the user to login page on landing, While user is not logged in.

for example :-

example.com ->(not logged in)-> redirect to login page.

example.com ->(logged in)-> redirect to Home page.

How can i do this ?

I was found some function like this

public function preDispatch()
{
    parent::preDispatch();

    if (!Mage::getSingleton('customer/session')->authenticate($this)) {
        $this->setFlag('', 'no-dispatch', true);
    }
}

How can i use or where should i use this.

Hope some will have experience on this. Thanks in advance

Answer

Gowri picture Gowri · Jan 10, 2012

This one save is me,

if(!$this->helper('customer')->isLoggedIn()){

  Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account'));

}

And i called it in cms page block.Hope this help someone.