3

So, I need to redirect user to some page(tt can be CMS or customer/account) if he did not logged. And on this page(CMS or customer/account) I will have a form to login/register. So, I made an observer for controller_action_predispatch event and method which will be implemented it, I did:

public function redirectNotLogged(Varien_Event_Observer $observer)
 {
 if(! Mage::helper('customer')->isLoggedIn()){
 Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account'));
 }
 }

But I have a problem, I cannot get to this page, because it infinitely redirect me. Any advices?

Marius
199k55 gold badges431 silver badges837 bronze badges
asked Feb 28, 2014 at 8:00

2 Answers 2

10

You are getting the infinite loop, because when you redirect to Mage::getUrl('customer/account') your observer is called again and redirects you to the same page.
You need to add a rule for when the redirect is not made.
Something like this.

public function redirectNotLogged(Varien_Event_Observer $observer)
{
 $action = strtolower(Mage::app()->getRequest()->getActionName());
 $controller = strtolower(Mage::app()->getRequest()->getControllerName());
 $openActions = array(
 'create',
 'createpost',
 'login',
 'loginpost',
 'logoutsuccess',
 'forgotpassword',
 'forgotpasswordpost',
 'resetpassword',
 'resetpasswordpost',
 'confirm',
 'confirmation'
 );
 if ($controller == 'account' && in_array($action, $openActions)) {
 return $this; //if in allowed actions do nothing.
 }
 if(! Mage::helper('customer')->isLoggedIn()){
 Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'));
 }
}
answered Feb 28, 2014 at 8:06
5
  • Did, but it also redirect infinitely.. Commented Feb 28, 2014 at 8:14
  • @sergio. Like I said, the code is not tested. Is just an idea. You can debug the code above to see why you get the second redirect. Commented Feb 28, 2014 at 8:15
  • @sergio. Ok I tested the code. There was an error in my method. I fixed it now. See my answer update. It works fine for me. Commented Feb 28, 2014 at 8:19
  • Hm, probably I did not understand something, but I copy code, and it also reload infinitely(magento is clear(new)) Commented Feb 28, 2014 at 8:37
  • @sergio Clear the cache. And make sure you copy the full method. I changed the redirect page from customer/account to customer/account/login. Commented Feb 28, 2014 at 8:48
3

An improved solution:

  • redirect for every route except "customer". This will not interfer with login, create account, password reset... and other customer actions already redirect to the login anyway.
  • set the no-dispatch flag. This is important because otherwise the action is executed and the page rendered even though it will not be shown to the user
  • use Mage_Customer_Model_Session::authenticate() to check the login and redirect. This will also save the currently requested URL in the session and redirect back after login

Code:

public function redirectNotLogged(Varien_Event_Observer $observer)
{
 $controller = $observer->getControllerAction();
 if ($controller->getRouteName() === 'customer') {
 return;
 }
 if (! Mage::getSingleton('customer/session')->authenticate($controller)) {
 $controller->setFlag('', 'no-dispatch', true);
 }
}

If you want to use a custom login page instead of customer/account/login, you can pass this custom URL as second parameter to authenticate(), but you also have to check for this action additionally to not get an infinite redirect loop again:

if ($controller->getFullActionName() === 'your_custom_page') {
 return;
}

and

authenticate($controller, Mage::getUrl('your/custom/page'))
answered Nov 26, 2015 at 21:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.