Wrong URL redirect after login CakePHP

909 views Asked by At

I’m working on a project using CakePHP and I’m having a little problem. When an unauthenticated user tries to access to a restricted page, they are redirected to the login page, which is a normal behavior on the security management of CakePHP using the Auth Component.

However, once the user logs in the application, it redirects the user to the wrong url. I.E. Instead of redirecting the user to the url it tried to access before, the application redirects the user to something like: localhost/myApplication/folder/myApplication/folder/urlUserTriedToAccess.

For some reason, the path of the project has been written twice and I don´t know why. I hope someone could give me a hint of what could be happening. Thanks a lot!

This is the code in the AppController where I set the values for the Auth component:

public $components = array(
  'DebugKit.Toolbar',
  'Session',
  'Auth' => array(
    'loginRedirect' => array('controller' => 'users', 'action' => 'home'),
    'logoutRedirect' => array('controller' => 'users', 'action' => 'home'),
    'authError' => "Debes iniciar sesión para entrar en esta página",
    'authorize' => array('Controller') // Added this line
));

This is the code in the Login action within the UsersController:

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash('Invalid user', 'default', array('class'=>'alert alert-error'));
        }
    }
}
1

There are 1 answers

1
met.lord On BEST ANSWER

You could try something like the code below after logging in to verify the redirect is working properly:

$this->redirect(array('controller'=>'anyRestrictedController','action'=>'index'));

Also, try using redirectUrl() instead of redirect().