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;
$user = Auth:user();
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
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.
protected $middlewareGroups = [
'web' => [
\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' => [
'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,
],
];
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.
I am using Laravel 5.2