Any way to prefix cookie for that domain. https://laravel.com/api/9.x/Illuminate/Cookie/CookieValuePrefix.html
Using Sanctum on multiple subdomains with XSRF-TOKEN cookie
Hey all. I have been working on a mega project for months, which is the primary reason for my inactivity in the forums. I encountered a problem today that I've been spinning my wheels on for hours, so I hope someone can point out where I've gone wrong.
Laravel API backend with Sanctum. Vue SPA front end with Axios.
I have 3 applications. They all share the same top level domain. Let's call it foo.com
App #1: (main office employee app and API behind a firewall)
foo.com (client)
api.foo.com
App #2: (public API and app for field employees)
field.foo.com (client)
api.field.foo.com
App #3 (public customer portal)
portal.foo.com (client)
api.portal.foo.com
App #1 and #2 share the same session cookie using Sanctum authentication. No problems here.
App #3 has its own set of users and session configuration. It is configured to have its own Sanctum stateful (sub)domain and basically be a stand-alone application.
Here is what I observe happening. When logging into App #3, the client makes its request to the sanctum/csrf-cookie endpoint and gets the token in the response. But, when the client sends the subsequent login post request (or any other post request), it is not sending the XSRF-TOKEN for the portal.foo.com subdomain. Instead, it is sending the top-level foo.com XSRF-TOKEN cookie (set by one of the other apps). This obviously leads to a CSRF mismatch exception being thrown. (for the sake of completeness, if the other two apps were never visted, then app #3 would work fine. The problem is conflicting cookies)
If I set the SESSION_DOMAIN in App #3 to .portal.foo.com, the CSRF exception is thrown at login regardless. If I change it to .foo.com, (like the other 2 apps are configured) then user can login and the app is fully functional, BUT.. this can lead to problems if a user is concurrently using App #3 with the other apps, as they will be sharing the top level XSRF-TOKEN cookie among them. For example, A request to App #1 would set the cookie value, then a subsequent request in App #3 would overwrite it. Then if the user makes a post in App #1, it'll cause a CSRF mismatch. To be clear, this wouldn't be a problem in the production environment, as App #3 will be on its own top level domain. But, this is in a staging/sandbox environment, where I'm trying to run all 3 apps under the same server and domain to minimize provisioning extra servers.
Do I have something misconfigured on the backend? Is Sanctum designed to handle this kind of set up? I don't believe its CORS related, as that would be an error before it gets to the CSRF mismatch exception. Is it the front-end? Is it because Axios is sending the wrong XSRF-TOKEN cookie from the top level domain instead of the subdomain?
I've been using Sanctum with App #1 and #2 for months without issues. Basically, all I want to do is run this stand-alone #3 app on another subdomain without it conflicting with cookies. There must be something simple that I'm overlooking.
Any guidance would be appreciated. Thanks in advance for your time.
Please or to participate in this conversation.