CakePHP Auth login - User login information

CakePHP Auth user login FAQ: Help, how do I get access to the CakePHP user object (user information) after using the CakePHP Auth component for the recommended CakePHP user login process?

Earlier today I wrote about how to create a CakePHP user registration form. One of the first things you run into after a CakePHP user has been registered, and then logs in, is "Okay, my user is logged in, how do I get access to this CakePHP Auth user information?" I struggled with this for a while, so I thought I'd share what I found out.

CakePHP Auth login and user object information

After your user logs into your application using the CakePHP Auth login process, the user information you want -- username, user id, etc. -- will be accessible from the Auth object in your controller classes. In the example I wrote about earlier today, I create the CakePHP Auth object in my AppController class, and then all of my other controller classes extend from AppController, which means I can access the Auth object from all of my CakePHP controller classes.

Digging into the CakePHP AuthComponent documentation, I found that I could then access Auth user information like this from my CakePHP controller classes:

$user     = $this->Auth->user();
$username = $user['User']['username'];
$userid   = $user['User']['id'];

I don't know about you, but I found I couldn't go any farther in my new project until I had this CakePHP Auth user information. For example, the first thing my application needs to do is display a list of projects for the current user, and you can't do that very well without knowing the user id.

The CakePHP session and the PHP session object

One more note before I leave this tutorial ... I finally stumbled on this CakePHP Auth user login solution by digging into the standard PHP session object. I put some CakepHP debug code into one of my view files that looked like this:

debug($_SESSION);

and that's when I was able to see my username and user id were indeed buried in the standard PHP session, and it at least gave me the encouragement to keep digging through the CakePHP AuthComponent object to find my user login information.

CakePHP Auth login and user information

This section probably won't make sense to anyone else, but I wanted to put it here for my own information. This is part of a CakePHP controller method I was using when I was trying to understand the CakePHP Auth User object:

function index() {
    $this->Project->recursive = 0;
    $this->set('projects', $this->paginate());
    
    # AuthComponent: http://api13.cakephp.org/class/auth-component
    # (1) this approach gets me the entire Auth user.
    # for some reason this doesn't print all the user information down to 'alvin'
  
    # (1a) this shows 'alvin'
    $this->set('auth', $this->Auth->user());
    # (1b) this does not show 'alvin'
    #$this->set('auth', $this->Auth);
    
    # (2) user() returns an array which has username='alvin'
    # this does get me the username:
    $u = $this->Auth->user();
    $name = $u['User']['username'];
    $id = $u['User']['id'];
    #$this->set('auth', $name);

    # (3) can see "alvin" here with the PHP session object
    #     might be something like this in the session: $this->Session->check('User')
    #$this->set('auth', $_SESSION);
}

Again, sorry if that doesn't make sense to you, I just put it there for myself.

CakePHP user login and Auth object - Summary

I hope this CakePHP user login and Auth object tutorial/example has been helpful. As usual, if you have any questions or comments, just leave a note in the Comments section below.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.