geoffs3310
geoffs3310

Reputation: 14008

CakePHP Auth Component: How do I check if someone is logged in?

I'm using the auth component and I don't want the login page to be accessible once the user has logged in, it should only be accessed by anonymous users. What can I put in my login method to accomplish this? I've tried this:

if (isset($this->Auth->user('id')) {
    $this->redirect('/profile/');
}

But I get the following error:

Fatal error: Can't use method return value

Upvotes: 1

Views: 5256

Answers (2)

Brad Koch
Brad Koch

Reputation: 20267

The Auth Component actually provides a method for checking if the user is logged in.

$this->Auth->loggedIn();

There are a half dozen other ways to check, but this one is best because it abstracts away the implementation of the authentication session. Also, it's easier to read.

Upvotes: 11

Teej
Teej

Reputation: 12873

if ($this->Session->check('Auth.User')){
    $this->redirect('/profile/');
}

Upvotes: 3

Related Questions