Strange redirect issue when using Auth and admin prefix in CakePHP
I am using prefix admin
in Cakephp application for some admin views. I am also using Auth to restrict access to these views based on a field role
in the User table. Pretty standard.
The problem is that when an unauthorized user tries to navigate to, say, admin/users
(in which case the index action is not allowed), they are redirected to /admin/users/login
, which of course doesn't exist.
This does not happen with actions that do not have an admin prefix. They are doing very well.
Why are users being sent to a login that is appended with an admin prefix and a prohibited action?
source to share
Anyone still having trouble with this, according to the documentation, you can use an array or string in loginAction ( Documentation ).
Using an array and setting 'admin' => false was still giving me problems, so I tried using a string instead:
public $components = array(
'Auth' => array(
'loginRedirect' => array('controller' => 'dashboards', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'loginAction' => '/users/login',
'authorize' => array('Actions')
),
);
This led to the solution to my problem. Hope it works for you too.
source to share
You need to override the specific prefix in the routing array.
$this->Auth->loginAction = array(
'controller' => 'users',
'action' => 'login',
'admin' => false
);
or, if you are using multiple prefixes, you can dynamically remove the prefix name like this:
$this->Auth->loginAction = array(
'controller' => 'users',
'action' => 'login',
$this->request->prefix => false
);
source to share