tags:

views:

2239

answers:

3

Hey all,

I'm trying to get authentication working to my liking in a cake php app and running into a snag.

I want to let the user login from either the home page or from a dedicated login page. I'm using the Auth component to manage login and right now the login itself works. I am submitting the form on the home page to /Users/Login and it does log them in and create session. The problem is it then redirects the user back to the home page. I'd rather they redirect to the location specified in loginRedirect.

If i login from /users/login directly it does forward to loginRedirect. I think the problem has something to do with posting the form from one page to another page instead of to itself, auth automatically thinks you want to go back to the previous page.

Any thoughts?

A: 

Yes auth has a feature where it will redirect you to the page you tried to access before logging in. If setting the loging redirect did not work, you can try to set the loginRedirect to false and do a manual ($this->redirect([..] ) in the UsersController::login action.

Alexander Morland
A: 

Hello Josh,

you can either turn off $autoRedirect by setting it to false and handling the redirect by yourself. The problem with the AuthComponent is, that there is too much automagic which you can't really control, or only by hacks.

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

$this->Session->del('Auth.redirect');
rscherer
+5  A: 

in the AppController

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

in UsersController

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

Also, if you haven't already you should move the form into an element, so that you can make absolutely certain that the login form is identical between the 2 login views.

Cleaning up a bunch of very old questions i never did an accept on. My apologies. I later did this again for another project and this suggestion worked. Not sure why i had problems the first time (which is why i never accepted)
JoshReedSchramm