fix: enable CORS credentials and externalize frontend origin for Sanctum SPA auth#541
Merged
zigzagdev merged 4 commits intoJul 20, 2026
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## fix/cors-credentials #541 +/- ##
=======================================================
Coverage 62.21% 62.21%
Complexity 1604 1604
=======================================================
Files 135 135
Lines 8252 8252
=======================================================
Hits 5134 5134
Misses 3118 3118 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Investigating the reported bug "user mypage does not display after login" found two issues in the CORS/Sanctum configuration:
config/cors.phphadsupports_credentialsset tofalse. Sanctum's cookie-based SPA auth requires the frontend to send requests withcredentials: 'include', and the browser only forwards/accepts the session cookie cross-origin if the server responds withAccess-Control-Allow-Credentials: true. With itfalse, login appeared to succeed (the cookie was issued) but subsequent authenticated requests (e.g. fetching the current user for mypage) were silently blocked by the browser.allowed_origins(http://localhost:3876, plus a stalehttp://127.0.0.1:5173entry left over from Vite's default port), which drifted from the actual frontend dev port and from the frontend's ownVITE_API_BASE_URLenv-based configuration.Closes #540
What I have done
supports_credentialstotrueinconfig/cors.phpand added a comment explaining why it's required for Sanctum cookie authallowed_originswithenv('FRONTEND_URL', 'http://localhost:3876')FRONTEND_URLto.env.example(and local.env, not committed) so both frontend and backend reference the same configurable originTest Results
docker exec heritage-app php artisan tinker --execute="print_r(config('cors'));"thatallowed_originsresolves to['http://localhost:3876', 'https://zigzagdev.github.io']andsupports_credentialsistrue(1)