0
votes

first to say I checked also into this answer and this other answer but it seems that doesn't solve the problem.

Expected result: after a successful login, redirect to "/admin/dashboard/index"

Actual result: after a successful login the application redirects to "/users/login". Login is done right, I can see it in the session. Also, when manually navigating to "/admin/dashboard" in the browser it redirects to "/admin" then following the rule defined in Config/routes.php it goes in infinite redirect loop

Here the relevant code:

in Config/core.php

Configure::write( 'Routing.prefixes', array( 'admin' ) );

in Config/routes.php

Router::connect( '/admin', array( 'controller' => 'dashboard', 'action' => 'index', 'admin' => true ) );

This is in my login action in UsersController

if ( $this->request->is( 'post' ) ) {
    if ( $this->Auth->login() ) {
        $this->redirect( $this->Auth->redirectUrl() );
    } else {
        $this->BASession->err( __( 'Login error. Please check your data!' ) );
    }
}

And, finally, this is my Auth configuration

    $this->Auth->loginAction = array( 'controller' => 'users', 'action' => 'login', 'admin' => false );
    $this->Auth->logoutRedirect = array( 'controller' => 'users', 'action' => 'login', 'admin' => false );
    $this->Auth->loginRedirect = array( 'controller' => 'dashboard', 'action' => 'index', 'admin' => true );
    $this->Auth->autoRedirect = true;

Relevant to say that changing with

$this->Auth->autoRedirect = false;

Does not makes any difference.

A DashboardController class with an empty "admin_index" function exists, even a blank view.

In the login function, debugging "$this->Auth->redirectUrl()" displays "/admin"

PHP version 5.4.10 - cakePHP version 2.5.1

I hope I have given sufficient information, if not, ask for them.

thank you!

1
try to set the Auth parameters in the component variable in your AppControler instead of setting them trought Auth object public $components = array('Auth' => array('loginAction' => array('controller' => 'users', 'action' => 'login', 'plugin' => false),'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index', 'prefix' => 'admin'), 'authError' => 'You shall not pass')); Sorry, i'm not good with the stack markdonw formatting - hidewak
Have you configured any authorization for Auth component? - ADmad
hey @ADmad you made it! it's working now by setting "return true" in the "isAuthorized" function inside "DashboardController". anyway, that's quite strange, i thought the problem was related to authentication issues and not authorization ones. but...good catch! still i can't figure why it was acting that way, but i'll dig into it. an aside question... is there a way to tell the application where to redirect if authorization fails? - paolaccio
"$this->Auth->redirectUrl()" displays "/admin" This was the hint that authentication and routing was working fine. So the next logical problem was authorization :) If authorization fails a message is set in session. Using DebugKit you can easily check the session values. Plus if you are authenticated $this->Auth->user() would return the user record irrespective of authorization info. - ADmad
Btw the isAuthorized() should be in AppController not a specific controller. - ADmad

1 Answers

-1
votes

Write below code in the AppController.php

public function beforeFilter() {
      $this->Auth->loginAction = array( 'controller' => 'users', 'action' => 'login', 'admin' => false );
      $this->Auth->logoutRedirect = array( 'controller' => 'users', 'action' => 'login', 'admin' => false );
      $this->Auth->loginRedirect = array( 'controller' => 'dashboard', 'action' => 'index', 'admin' => true );

}

login action in UsersController

if ( $this->request->is( 'post' ) ) {
    if ( $this->Auth->login() ) {
       $this->redirect($this->Auth->redirect());
   } else {
      $this->Session->setFlash(__('Your username or password was incorrect.'));
   }
 }