Here is a tutorial for it released this week https://laracasts.com/series/whats-new-in-laravel-7/episodes/6
Using Sanctum with a Vue SPA
Hi,
I am trying to use Sanctum in my local development environment (using Homestead). I have set up two projects:
- An API backend using Laravel 7 (latest version) running at backend.smi.local:8000
- A SPA frontend using Vue CLI 3 running at localhost:8080 if I compile for development and at admin.smi.local:8000 if I run a build version
I added Laravel Sanctum to my backend project using composer require laravel/sanctum, published the sanctum configuration file and ran the migration. And of course I added the middleware to Kernel.php: 'api' => [ EnsureFrontendRequestsAreStateful::class, 'throttle:60,1', \Illuminate\Routing\Middleware\SubstituteBindings::class, ],
I went through all settings needed for Sanctum:
sanctum.php
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', 'localhost,127.0.0.1')),
'expiration' => null,
'middleware' => [
'verify_csrf_token' => App\Http\Middleware\VerifyCsrfToken::class,
]
cors.php
'paths' => ['api/*', 'sanctum/csrf-cookie', 'login'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,
session.php
'domain' => env('SESSION_DOMAIN', null),
And within the Laravel .env file:
SESSION_DOMAIN=.smi.local:8000
SANCTUM_STATEFUL_DOMAINS=localhost:8080,admin.smi.local:8000
Within my SPA frontend I added axios via the Vue UI as a plugin. This results in a axios.js file within the plugin folder where you can maintain all central settings for axios. The only thing I changed in the axios.js installed by default is the definitions in the config-variable like so:
let config = {
baseURL: process.env.VUE_APP_API_URL,
withCredentials: true,
};
In the .env.development file in this project I added:
VUE_APP_API_URL=http://backend.smi.local:8000
Within the login-function of the login form of the SPA I added this function:
axios.get('/sanctum/csrf-cookie')
.then(response => {
console.log(response);
});
And here it goes wrong. In stead of a successful request I get:
Access to XMLHttpRequest at 'http://backend.smi.local:8000/sanctum/csrf-cookie' from origin 'http://admin.smi.local:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
or
Access to XMLHttpRequest at 'http://backend.smi.local:8000/sanctum/csrf-cookie' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
By now I have gone over all the settings about a zillion times. Tried it with and without the port-setting, with and without http:// in front of the STATEFUL_DOMAINS variable and I don't know what else but to no avail. I can't get the communication between my SPA and backend to work. I must be overlooking something but I can't seem to figure out what.
Any help would be much appreciated.
Edwin
I guess I will never know what went wrong. My final attempt was deleting the entire backend project and build it from scratch. And know it all works as expected. Apparently I made a mistake of something went wrong in the former installation.
Please or to participate in this conversation.