title: Lost Auth sessions in CakePHP Jquery Ajax calls
body: <p>I made frequent Jquery Ajax calls (post, load) with my CakePHP app. Normally loading content into divs, and the content is related with the user who is logged in.</p>
  
  <p>I use the default Form authentication, and define the logged in user in the AppController:</p>
  
  <pre><code>$this-&gt;aU = $this-&gt;User-&gt;findById($this-&gt;Auth-&gt;user('id'));
  </code></pre>
  
  <p>After this I normally use the logged in user in controllers with $this->aU and it works fine in normal view rendering.
  But when I render views with ajax and setinterval, 1 time in 20 the call lost the session and the controller redirects the request, because cannot see the logged in user.</p>
  
  <pre><code>var interval = setInterval(function() {
    $('#alerts').load('&lt;?php echo $this-&gt;Html-&gt;url('/users/alerts'); ?&gt;');
  }, 20000);
  </code></pre>
  
  <p>In UsersController alerts action I use this:</p>
  
  <pre><code>public function alerts() {
    $this-&gt;layout = 'ajax';
    $user = $this-&gt;aU;
    $this-&gt;loadModel('Message');
    $unread_messages = $this-&gt;Message-&gt;find('count', array(
      'conditions' =&gt; array('receiver_user_id' =&gt; $user['User']['id'])
    ));
    $this-&gt;set(compact('user', 'unread_messages'));
  }
  </code></pre>
  
  <p>Maybe I use wrong logic in user identification?</p>