title: Checking logged-in user info on CakePHP when using a custom auth adapter
body: <p>I'm using <a href="https://github.com/t73biz/cakephp2-jwt-auth" rel="nofollow noreferrer">this JWTAuth adapter</a> to use JWT authentication instead of cookie-based auth in my CakePHP 2.8 app. It works great, except for one hitch:</p>
  
  <p>Normally for one of my REST endpoints, I can use <code>$this-&gt;Auth-&gt;user("id")</code> to get the currently logged-in users' ID.</p>
  
  <p>When I try to make a controller action accessible to non-members using <code>$this-&gt;Auth-&gt;allow()</code>, a problem occurs. If I do this, using <code>$this-&gt;Auth-&gt;loggedIn()</code> in the controller returns <code>false</code>, meaning I can not add additional logic for logged-in users.</p>
  
  <p><strong>When using standard cookie auth:</strong></p>
  
  <ul>
  <li><code>$this-&gt;Auth-&gt;user('id')</code> is available in
  <code>Controller::beforeFilter()</code>.</li>
  <li><code>$this-&gt;Auth-&gt;loggedIn()</code> is <code>true</code> in
  <code>Controller::beforeFilter()</code>.</li>
  <li><code>$this-&gt;Auth-&gt;user('id')</code> is available in controller actions, public
  and members-only.</li>
  <li><code>$this-&gt;Auth-&gt;loggedIn()</code> is <code>true</code> in controller actions, public
  and members-only.</li>
  </ul>
  
  <p><strong>When using JWT auth:</strong></p>
  
  <ul>
  <li><code>$this-&gt;Auth-&gt;user('id')</code> is <code>null</code> in <code>Controller::beforeFilter()</code>.</li>
  <li><code>$this-&gt;Auth-&gt;loggedIn()</code> is <code>false</code> in
  <code>Controller::beforeFilter()</code>.</li>
  <li><code>$this-&gt;Auth-&gt;user('id')</code> is available in members-only controller actions, and <code>null</code> in public controller actions.</li>
  <li><code>$this-&gt;Auth-&gt;loggedIn()</code> is <code>true</code> in members-only controller actions, and <code>false</code> in public controller actions.</li>
  </ul>
  
  <p>Is there any way I can get Auth to include information returned by the JWTAuth component on actions that have been made public by <code>$this-&gt;Auth-&gt;allow()</code>?</p>
  
  <p><strong>Example controller here:</strong></p>
  
  <pre><code>public function visible(){
      // This will always be false, even if a valid JWT token is sent
      $this-&gt;set("loggedIn", $this-&gt;Auth-&gt;loggedIn());
  }
  
  public function members_only(){
      // This will be unavailable if not logged in, and a true if logged in
      $this-&gt;set("loggedIn", $this-&gt;Auth-&gt;loggedIn());
  }
  
  public function beforeFilter($options = array()){
      parent::beforeFilter();
  
      $this-&gt;Auth-&gt;allow("visible");
  }
  </code></pre>
  
  <p><strong>And for reference, my AppController::components array;</strong></p>
  
  <pre><code>public $components = array(
      'DebugKit.Toolbar',
      'Auth' =&gt; array(
          'authorize' =&gt; array(
              'Actions' =&gt; array(
                  'actionPath' =&gt; 'controllers'
              ),
          ),
          'authenticate' =&gt; array(
              'Form' =&gt; array(
                  'fields' =&gt; array('username' =&gt; 'email'),
                  'contain' =&gt; array(
                      'UserProfile',
                  )
              ),
              'JwtAuth.JwtToken' =&gt; array(
                  'fields' =&gt; array(
                      'username' =&gt; 'email',
                      'token' =&gt; 'password',
                  ),
                  'header' =&gt; 'AuthToken',
                  'userModel' =&gt; 'User',
              ),
          ),
          'unauthorizedRedirect' =&gt; false
      ),
      "Acl",
      "RequestHandler",
      "Session"
  );
  </code></pre>