1
votes

I want users to be redirected to the posts index action upon login. But they instead get redirected to posts's add action. below is my routes.php

    Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
/**
 * ...and connect the rest of 'Pages' controller's urls.
 */
    Router::connect('/pages/*', array('controller' => 'posts', 'action' => 'index'));

and my login action in users controller is

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

** DEBUG **

when I did debug upon login, I get this

/app/Controller/UsersController.php (line 20)
object(AuthComponent) {
    components => array(
        (int) 0 => 'Session',
        (int) 1 => 'RequestHandler'
    )
    authenticate => array(
        (int) 0 => 'Form'
    )
    authorize => array(
        'Actions' => array(
            'actionPath' => 'controllers'
        )
    )
    ajaxLogin => null
    flash => array(
        'element' => 'default',
        'key' => 'auth',
        'params' => array()
    )
    loginAction => array(
        'controller' => 'users',
        'action' => 'login'
    )
    loginRedirect => array(
        'controller' => 'posts',
        'action' => 'add'
    )
    logoutRedirect => array(
        'controller' => 'users',
        'action' => 'login'
    )

for some reason it says

loginRedirect => array( 'controller' => 'posts', 'action' => 'add' )

instead of action => index it is unsing action => add. Whys that

1
Did you set/override the Auth component's variables? - Ehtesham

1 Answers

1
votes

Auth->redirect() returns the url where the user landed before being taken to the login page or Auth->loginRedirect.

https://stackoverflow.com/a/2636940/643500

So, if you set the root path to the index it might do the trick.