1

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!

Rogier
  • 33
  • 5
  • Does this answer your question? [Get 401(Unauthenticated) error after login on laravel sanctum with nuxtjs](https://stackoverflow.com/questions/68266276/get-401unauthenticated-error-after-login-on-laravel-sanctum-with-nuxtjs) – Abishek Jul 19 '21 at 17:17
  • Unfortunately I already have and/or tried everything there – Rogier Jul 19 '21 at 22:56
  • Not sure if this one may help you: https://stackoverflow.com/a/68081536/8816585 – kissu Jul 20 '21 at 00:04

0 Answers0