My application is set up to always redirect to a login screen. The login and logout redirects are driving me nuts though.
When I actually login, first it will redirect to the logout URL, then if I login a second time it will redirect to the home page correctly. Then when I log out, it redirects to the login URL and not the logout URL.
In the app_controller.php
public function beforeFilter() {
$this->Auth->userModel = 'User';
$this->Auth->loginAction = '/users/login';
$this->Auth->loginRedirect = '/home';
$this->Auth->logoutRedirect = '/users/login/1';
$this->Auth->authError = 'You must be logged in to view this page.';
}
And in the users_controller.php
public function login($loggedout = false) {
if ($this->Session->check('Message.auth')) {
$this->Session->setFlash('Incorrect username or password.', 'default', array('class' => 'msg error'), 'auth');
} elseif ($loggedout) {
$this->Session->setFlash('You have been logged out.', 'default', array('class' => 'msg success'), 'auth');
}
}
/**
* Logout action
*/
public function logout() {
$this->redirect($this->Auth->logout());
}
I have no idea what is going wrong. It seems like a pretty simple component to use. I am new to CakePHP by the way.
basically, login should redirect to /home, and logout should redirect to /users/login/1 so I can display the “you have been logged out” message above the login form again. That is literally all I need to do.
I don’t have a ton of experience with the Auth component, but I suspect you’re making this a little more difficult than it should be. The documentation is clear that, by default, Auth will block access to all actions except
login()andlogout().I’d start by removing the
userModelandloginActiondeclarations (you’re specifying the defaults anyway). Also, you can probably remove all of the code inlogin()(leave the empty function). You can display any error messages from the Auth compoment in your view—include this in your login.ctp:Note that the Auth component will generate messages like the ones you have in your code. It’s probably easier just to let it do its thing, and only override that behavior where you really need to.
I would also (temporarily) remove the
loginRedirectandlogoutRedirectdeclarations and see if things behave as expected. (By default, logins will redirect back to whatever page you were trying to access before you logged in. Logouts will redirect to the login page, with a flash message indicating you’ve been logged out.) If you need to change the default behaviors, add them back one at a time and test.Basically, since you’re using the User model that Cake expects by default, you hardly need any configuration at all—which is one of the nice things about CakePHP.