Before start, I'd like to say that I searched and tried many approaches and none of them seem to work properly. I also created a fresh Laravel installation but still, no success.
The problem is CORS, I get blocked by CORS always.
- The setup uses
Laravel Valet
. - The Laravel Application runs at
https://sanctum.test
- The NuxtJs Application runs at
https://nuxt.sanctum.test
It's a fresh installation.
My .env
file has the required settings:
SESSION_DOMAIN=.sanctum.test
SANCTUM_STATEFUL_DOMAINS=sanctum.test,nuxt.sanctum.test
My cors.php
has the required settings:
...
'supports_credentials' => true,
...
My Http/Kernel.php
...
'api' => [
EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
My NuxtJs App runs on http://localhost:3000 and I have a Laravel Valet reverse proxy so I can access it from https://nuxt.sanctum.test
and I can confirm that's working as expected.
Here's my nuxt.config.js
:
export default {
ssr: false,
target: 'server',
head: {
title: process.env.npm_package_name || '',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{
hid: 'description',
name: 'description',
content: process.env.npm_package_description || '',
},
],
link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
},
css: [],
plugins: [],
components: true,
buildModules: ['@nuxtjs/eslint-module', '@nuxtjs/tailwindcss'],
modules: [
'@nuxtjs/axios',
'@nuxtjs/auth-next',
'@nuxtjs/pwa',
'@nuxtjs/dotenv',
],
auth: {
strategies: {
laravelSanctum: {
provider: 'laravel/sanctum',
url: 'https://sanctum.test',
},
cookie: {
cookie: {
name: 'XSRF-TOKEN',
},
},
},
},
router: {
middleware: ['auth'],
},
axios: {
proxy: true,
credentials: true,
},
proxy: {
'/laravel': {
target: 'https://sanctum.test',
pathRewrite: { '^/laravel': '/' },
},
},
build: {},
}
Now, no matter what I do, I always get the same error from the Laravel Application:
Access to XMLHttpRequest at 'https://sanctum.test/sanctum/csrf-cookie' from origin 'https://nuxt.sanctum.test.test' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
As you can see above, the request is being made from the subdomain to the main domain. And as I showed above, the SANCTUM_STATEFUL_DOMAINS
is correct.