title: Access logged in user via Ajax in Laravel
body: <p>I have read a lot about this but I can't seem to solve it, so I am just going around in circles. I am trying to access a logged user via an API route I have setup using;</p>
  
  <pre><code>$user = Auth:user();
  </code></pre>
  
  <p>The problem is; $user is always null when I make an ajax request but the user is not null when I access the API route in the browser. For example entering; api.example.com/v1/myaccount</p>
  
  <p>I have solved many problems along the way such as turning on sessions for all subdomains and adding the following to the api route middleware.</p>
  
  <pre><code>    protected $middlewareGroups = [
      'web' =&gt; [
          \App\Http\Middleware\EncryptCookies::class,
          \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
          \Illuminate\Session\Middleware\StartSession::class,
          \Illuminate\View\Middleware\ShareErrorsFromSession::class,
          \App\Http\Middleware\VerifyCsrfToken::class,
      ],
  
      'api' =&gt; [
          'throttle:60,1',
          \App\Http\Middleware\EncryptCookies::class,
          \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
          \Illuminate\Session\Middleware\StartSession::class,
          \Illuminate\View\Middleware\ShareErrorsFromSession::class,
          \App\Http\Middleware\VerifyCsrfToken::class,
      ],
  ];
  </code></pre>
  
  <p>Previously the url was returning a 404 error via Ajax but worked fine in the browser. Now the $user variable is just empty as if no user is logged. But I have logged normally.</p>
  
  <p>I am using Laravel 5.2</p>