I have been stuck at this for hours just to find that it might be a bug from the Sanctum package itself.
I have the endpoint below:
// routes/api.php
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
It keeps returning 401 unauthenticated message in my Nuxt app even after a successful login. If I try to login again the /api/login endpoint does detect that I'm already logged in and tries to redirect me. The only way I managed to get it to work is by commeting out throttle:api in the following:
// app/Http/Kernel.php
'api' => [
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
// 'throttle:api',
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
I initially found the solution through an old GitHub issue (https://github.com/laravel/sanctum/issues/369). Seems like the issue was never solved or discussed since I couldn't find any other posts about it. Thanks in advance