Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jeroenvip's avatar

Cross site login 419 CSRF token mismatch.

Good evening, thank you in advance for taking the time to read up on this problem. I am creating a laravel app where remote login for users is required.

The login in the application itself works. The remote login is not.

The laravel app is running on https://apptabai.local. The remote application, a simple index.php file with a form runs on https://remote.login.local.

Cors

Cors Output

macbookpro2017@MacBookPro:~/oldIcloud/localdev/laravel/apptabai-local$ php artisan config:show cors

  cors ............. 
  paths ⇁ 0 ........................... *  
  allowed_methods ⇁ 0 ......... *  
  allowed_origins ⇁ 0 .......... https://remote.login.local  
  allowed_origins ⇁ 1 ........ https://apptabai.local  
  allowed_origins_patterns ............ []  
  allowed_headers ⇁ 0 ............ *  
  exposed_headers .................................. []  
  max_age ......................... 0  
  supports_credentials .............................. true  

Login form on the second domain

Request https://apptabai.local/auth/remote/csrf-token

GET /auth/remote/csrf-token HTTP/1.1
Pragma: no-cache
Accept: */*
Sec-Fetch-Site: cross-site
Accept-Language: en-US,en;q=0.9
Accept-Encoding: gzip, deflate, br
Sec-Fetch-Mode: cors
Cache-Control: no-cache
Origin: https://remote.login.local
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15
Referer: https://remote.login.local/
Connection: keep-alive
Host: apptabai.local
Sec-Fetch-Dest: empty

Response https://apptabai.local/auth/remote/csrf-token

HTTP/1.1 200 OK
Content-Type: application/json
Access-Control-Allow-Origin: https://remote.login.local
Set-Cookie: XSRF-TOKEN=eyJpdiI6Ilp2a2xmV1g2NmVTU3FiOTFBMEc2V2c9PSIsInZhbHVlIjoiYWdNaHpIajhXYUNlYlB4a1VtemwvYklOdzVvRnJhL2pXZEl3NkVBOSt1MngzRHorQ1pQbE9Ha2xBeVE2RWxuQlJlOVhSVG9FT2hZbVFIU2NjYnpkNXlhMHJmSndMeThIN2dKN1djcVJBcldaQy9pUjUyVXZ1ODhBYVdJNFNGNmkiLCJtYWMiOiI5YmZhMzJlOGYwN2QzZDI3MjVlZTdmZDc3NzgxZWM3ZWNmZjQ2MmM1YjA2YjFiNTRlZDhlZTU0YzQ5YjE0NDhiIiwidGFnIjoiIn0%3D; expires=Mon, 10 Mar 2025 02:00:55 GMT; Max-Age=7200; path=/; secure; samesite=lax, laravel_session=eyJpdiI6InNMVmxqdjBURE1FYjlsdnF3OGxpVHc9PSIsInZhbHVlIjoiY1Z0UnZqTCs1dnpvL0k3T1dqWGhxUmNha2tGcmxUY1BRZ21pOGtPUmhHcW0wSURkalZhZHhFazZ5ZTBoTE5PcWZWVWN1R3NmZVJaRWZBT3E0dGtybmtmSUprWno4d01FYk8zaktYV1NHRzUxRU9RSE5oRDUzQkpYeTZ5WTBodVgiLCJtYWMiOiIxZWQ3MGVmOTg3MmZlNDI1Mjc4MjE4MGY3MjkzZWZlY2E1MjFhN2E3ZGQ2MTIzMDZhMTk2Mzg5M2YxOTFjZjc0IiwidGFnIjoiIn0%3D; expires=Mon, 10 Mar 2025 02:00:55 GMT; Max-Age=7200; path=/; secure; httponly; samesite=lax
Transfer-Encoding: Identity
Cache-Control: no-cache, private
Date: Mon, 10 Mar 2025 00:00:55 GMT
Access-Control-Allow-Credentials: true
Connection: keep-alive
Vary: Origin
X-Powered-By: PHP/8.2.27
Server: nginx/1.27.4
X-RateLimit-Remaining: 9
phpdebugbar-id: 01JNYNNHHCBS1B3S8WABQ42K8G
X-RateLimit-Limit: 10

And this is where is goes wrong

Request https://apptabai.local/auth/remote/login

POST /auth/remote/login HTTP/1.1
Content-Type: application/json
Pragma: no-cache
Accept: application/json
Sec-Fetch-Site: cross-site
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Sec-Fetch-Mode: cors
Accept-Encoding: gzip, deflate, br
Origin: https://remote.login.local
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15
Referer: https://remote.login.local/
Content-Length: 100
Connection: keep-alive
Sec-Fetch-Dest: empty
Host: apptabai.local
X-CSRF-TOKEN: afoZpPE61R0dLtncLGBI4tycT002OgWKjEX8Qlu0

Request data

{"email":"[email protected]","password":"password","_token":"afoZpPE61R0dLtncLGBI4tycT002OgWKjEX8Qlu0"}

Response https://apptabai.local/auth/remote/login*

HTTP/1.1 419 unknown status
Content-Type: application/json
Access-Control-Allow-Origin: https://remote.login.local
Set-Cookie: laravel_session=eyJpdiI6InN0akI5aCs1b0tFUHdXQnhDSWcrd0E9PSIsInZhbHVlIjoiWUliYWVyTTlpWmhaWXE2RnE3S1JLZnZ2TmRPT2VFei9rN3dSK2s4d25SbGJPOXFtNnpsZFlUYStQS3dMK1lTb2tRUnJmSStKcUlOaEtoUUcxU1lMcXAxV3k2VlJxcmNlMGF3NnN6M0JtK2kraE9rRjg4NTJJUk9VUDJ4Q3pkSzEiLCJtYWMiOiJlMTdjMDFlYzM4N2M2YmNiMDI0NDU5NWI2MDdkNGMxMjNjYzNkYmM3ZmFkZThmMTNjNDViZjRiYzQyMTRiYTNlIiwidGFnIjoiIn0%3D; expires=Mon, 10 Mar 2025 02:04:03 GMT; Max-Age=7200; path=/; secure; httponly; samesite=lax
Transfer-Encoding: Identity
Cache-Control: no-cache, private
Date: Mon, 10 Mar 2025 00:04:03 GMT
Access-Control-Allow-Credentials: true
Connection: keep-alive
Vary: Origin
X-Powered-By: PHP/8.2.27
Server: nginx/1.27.4
phpdebugbar-id: 01JNYNV8ZFR38ENP8K2BRPKZFK

Error 419 content

0 likes
1 reply
LaryAI's avatar
Level 58

The issue you're facing with the "CSRF token mismatch" error is common when dealing with cross-domain requests in Laravel. Here are some steps to resolve this issue:

  1. Ensure CORS is Properly Configured: Make sure that your CORS configuration allows requests from https://remote.login.local. You seem to have this set up, but double-check your config/cors.php file to ensure it's correct.

  2. CSRF Token Handling: When making cross-domain requests, the CSRF token needs to be correctly set and sent with the request. You are fetching the CSRF token and setting it in a hidden input field, which is correct. However, ensure that the token is being correctly retrieved and sent.

  3. Session Cookie: Laravel uses session cookies to validate CSRF tokens. Ensure that the session cookie is being sent with your requests. This requires the credentials: 'include' option in your fetch request, which you have already set.

  4. SameSite Cookie Attribute: The SameSite attribute of cookies can prevent them from being sent in cross-site requests. Ensure that your session cookies are set with SameSite=None and Secure attributes. You can configure this in your config/session.php file:

    'same_site' => 'none',
    'secure' => true,
    
  5. Check for Middleware: Ensure that the VerifyCsrfToken middleware is not excluding the route you're trying to access unless you have a specific reason to do so.

  6. Debugging:

    • Use browser developer tools to inspect the network requests and ensure that the CSRF token is being sent correctly.
    • Check the response headers to ensure that the session cookie is being set and sent back with the request.
  7. Testing Locally: If you're testing locally, ensure that your local environment is set up to handle HTTPS correctly, as cross-domain requests often require secure connections.

Here's a checklist to ensure everything is set up correctly:

  • Verify that the CSRF token is correctly fetched and set in the form.
  • Ensure that the session cookie is being sent with the request.
  • Check that the CORS configuration allows requests from the remote domain.
  • Ensure that the SameSite attribute is set to None and Secure is true in your session configuration.

If you follow these steps and ensure that all configurations are correct, the CSRF token mismatch issue should be resolved.

Please or to participate in this conversation.