I'm trying to use laravel Sanctum SPA with my Nuxt application, but I'm running into an issue. I followed all the steps in order to get it to work. I am able to successfully login and get the XSRF-token, but when I want to retrieve the actual logged in user after the login, it returns a 401 Unauthorized. Below all my configurations. My nuxt app is running on http://localhost:3000 and Laravel on http://127.0.0.1:8000 with php artisan serve
.env:
SANCTUM_STATEFUL_DOMAINS=localhost:3000
SESSION_DOMAIN=localhost
Kernel.php
'api' => [
EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
cors.php
'paths' => ['api/*', 'sanctum/csrf-cookie'],
'supports_credentials' => true,
routes/api.php
Route::middleware('auth:sanctum')->get('user', function (Request $request) {
return $request->user();
});
Route::post('login', [AuthController::class, 'login']);
nuxt.config.js
axios: {
credentials: true,
},
auth: {
strategies: {
'laravelSanctum': {
provider: 'laravel/sanctum',
url: 'http://localhost:8000',
endpoints: {
login: {
url: '/api/login',
method: 'post'
},
logout: {
url: '/api/logout',
method: 'get'
},
user: {
url: '/api/user',
method: 'get'
}
},
},
},
},
Does anyone here know what might be wrong or missing? Open to all solutions :). Thanks in advance!