I am facing this weird problem. I am trying to change the default loginRedirect of the admin role from that of normal user.
U have the auth key in the AppController's component variable set up as follows :
'Auth' => array(
'loginRedirect' => array(
'controller' => 'donors',
'action' => 'index'
)
)
Now in the beforeFilter callback I have this set up:
if($this->Auth->user('role') == 'admin'){
$this->Auth->loginRedirect = array(
'controller'=>'users',
'action'=>'admin_index',
'prefix'=>'admin',
'admin'=>true
);
}
However, this does not work and the if condition is never met. I am expecting this to run when the user logs in. If I add an else
condition and repeat the same code shown above, it works and the admin is redirect to the desired page.
Can anyone instruct how I am able to do this correctly ? Thanks in advance
If the user is not logged in,
$this->Auth->user()
will return null. beforeFilter() will run before any action is run, so your login() action has still not been called.Do the redirecting after
$this->Auth->login()
has been called and is successful. E.g. in yourUsersController::login()
action (or whichever action you use to login):Instead of
$this->Auth->loginRedirect
use$this->redirect( array('controller' => 'users', 'action' => 'admin_index');
Its less complicated