Intentando crear la aplicación Laravel Api que estaría en un dominio diferente con React como aplicación frontend... cuando intento iniciar sesión, después de obtener csrf-cookie y realizar otra solicitud, aparece el error:
{ "message": "Network Error", "name": "AxiosError", "config": { "transitional": { "silentJSONParsing": true, "forcedJSONParsing": true, "clarifyTimeoutError": false }, "transformRequest": [ null ], "transformResponse": [ null ], "timeout": 0, "xsrfCookieName": "XSRF-TOKEN", "xsrfHeaderName": "X-XSRF-TOKEN", "maxContentLength": -1, "maxBodyLength": -1, "env": { "FormData": null }, "headers": { "Accept": "application/json, text/plain, */*", "Content-Type": "application/json" }, "withCredentials": true, "method": "post", "url": "http://localhost:8000/api/login", "data": "{\"email\":\"test2@gmail.com\",\"password\":\"test\"}" }, "code": "ERR_NETWORK", "status": null }si deshabilito
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,En mi archivo Kernel.php, todo funciona bien
Así envío la solicitud
function submitForm(e) { e.preventDefault(); const body = { email: email, password: password, }; axios.get(csrf).then((res) => { console.log(res); axios.post(login, body, {withCredentials:true}).then((res) => { console.log(res); }); }); } Pero si {withCredentials:true} me sale este error:
mensaje: "Solicitud fallida con el código de estado 419" "No coincide el token CSRF".
Archivo Cors.php:
'paths' => ['*'], 'allowed_methods' => ['*'], 'allowed_origins' => ['*'], 'allowed_origins_patterns' => [], 'allowed_headers' => ['*'], 'exposed_headers' => [], 'max_age' => 0, 'supports_credentials' => true,