I have a Laravel backend and react frontend where I use sanctum for SPA authentication and Axios to make my requests. I am using localhost with my backend on http://localhost:8000 and frontend on http://localhost:3000.
Here is my frontend code:
const apiClient = axios.create({
baseURL: `http://localhost:8000`,
withCredentials: true,
});
let config = {
headers: {
'Content-Type': 'application/json'
},
}
export async function login(credentials: LoginCredentials) {
return apiClient.get('sanctum/csrf-cookie', config).then(response => {
apiClient.post('/api/v1/login', JSON.stringify(credentials));
});
}
my api middleware class looks like this
'api' => [
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
'throttle:api',
],
These are my sanctum configurations;
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
'%s%s',
'localhost,localhost:3000,localhost:3000/api/v1/login,127.0.0.1,127.0.0.1:8000,::1',
env('APP_URL') ? ','.parse_url(env('APP_URL'), PHP_URL_HOST) : ''
))),
SESSION_DOMAIN=http://localhost:3000
And finally my cors middleware
'paths' => ['api/*', 'sanctum/csrf-cookie'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,
I am pretty sure i am missing something minor. Any help would me appreciated