1

I know this must be something really stupid, but I am having issues with logging out. I can still see the full user Auth variable after calling Auth->logout(). In my users controller I have the standard:

function login()
{
}

function logout()
{
  $this->redirect($this->Auth->logout());
}

But when I call logout, in my view I can still print the User by doing this:

$auth = $this->Session->read('Auth.User');
print "<pre>";
print_r($auth);
print "</pre>";

Am I missing something basic here? Thanks!

1
  • pr($auth) does the same! and is faster to write
    – mark
    Aug 7, 2011 at 8:35

7 Answers 7

5

mine:

function logout() {
    $this->Session->destroy();
    $this->redirect($this->Auth->logout());
}
5

you have not allowed the use of the logout function, and the user is redirected instead of logged out.

in your controller containing logout the function, add this in your before filter:

    $this->Auth->allow('logout');
2
  • 2
    is an old question but anyone following the acl tutorial in cakephp 2.0 cookbook will end up with this problem...
    – joe.smack
    May 17, 2012 at 16:18
  • @joe.smack I've had tons of AuthComponent::allow() problems today from following the Cookbook tutorials. Jul 25, 2013 at 22:07
1

In your app controller you must define a loginAction, in case of a not authorized entry, the view is redirected to that URL

'Auth' => array(
    'loginRedirect' => array('controller' => 'products', 'action' => 'all'),
    'logoutRedirect' => array('controller' => 'products', 'action' => 'index'),
    'loginAction' => array('controller'=>'admins', 'action'=>'login'),
)
1

You'll find that if you just create a beforeFilter() function in UserController with that one line, you'll break the authorization on the Users model. That is, any user will be able to do users/add, users/edit, etc. To fix this, make sure you call AppController's beforeFilter. The complete beforeFilter() function looks like this:

public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('logout');
}
0

If CakePHP is using PHP sessions and not rolling their own, you could just clear out the session on logout via session_destroy();. Sorry I have no CakePHP experience, so I'm just going off of an assumption.

0

what cake version do you have? I think you have to manually clear session in Cake 1.2. In newer Cake, if the logout function is called, it would clear out Auth.User; I'm sure on that.

0
0

I can't see a reason why this shoudn't work as I use exactly the same code... did you confirm that the method is actually called? a simple "die('xyz')" etc before the Auth logout part can confirm that your action code is triggered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.