Eugene msc
Eugene msc

Reputation: 380

retrieving user information from auth component

I have problems checking if user is logged in from any controller other than UsersController.

I was trying to make a user profile page. I routed it to PagesController and when I use

$this->Auth->user()

it always returns null. But if I route it to UsersController it works fine. Why is that? Login seems to work ok.

Upvotes: 0

Views: 78

Answers (2)

GG.
GG.

Reputation: 21834

Your app_controller.php :

class AppController extends Controller {

    var $components = array('Auth', 'Session', 'Cookie');

    function beforeFilter() {
        $this->Auth->authError = 'blabla';
        $this->Auth->loginError = 'blabla';
    }

}

In your controllers :

function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('function1', 'function2', 'function3'); //etc
    $this->Auth->autoRedirect = true;
}

Upvotes: 1

Nik Chankov
Nik Chankov

Reputation: 6047

It's because you include the Auth component only in your User's controller. You should include it in AppController and in beforeFilter(). This way you will have access in all controllers.

Upvotes: 0

Related Questions