How to get loginRedirect from multiple login locations in CakePHP?

I am trying to get authentication working as I see fit in CakePHP application and getting confused.

I want the user to log in from the home page or from a separate login page. I use the Auth component to control login, and the login itself is working now. I submit the form on the home page to / Users / Login, and it logs them in and creates a session. The problem is that it redirects the user back to the home page. I would prefer that they redirect to the location specified in loginRedirect.

If I log in with / users / login directly, it forwards loginRedirect. I think the problem is with sending the form from one page to another, and not to itself, auth automatically thinks that you want to return to the previous page.

Any thoughts?

+5
source share
3 answers

in appcontroller

public function beforeFilter( )
{
    $this->Auth->autoRedirect   =   false;
}

in usercontroller

public function login( )
{
    if( $this->Auth->user( ) )
    {
        $this->redirect( array(
            'controller'    =>  'users' ,
            'action'    =>  'index' ,
        ));
    }
}

In addition, if you have not done so already, you must transfer the form to the element so that you can absolutely make sure that the login form is identical between the two input types.

+8
source

auth , , , . , loginRedirect false ($ this- > redirect ([..]) UsersController:: login.

0

you can disable $ autoRedirect by setting it to false and handle the redirection yourself. The problem with AuthComponent is that there is too much autogam that you cannot control, or just hacks.

One solution to your problem is to remove the Session.Auth.redirect key, so AuthComponent will always use the $ loginRedirect URL:

$this->Session->del('Auth.redirect');
0
source

All Articles