fix: enable Sanctum SPA cookie auth for the frontend origin / フロントのオリジンに対するSanctum Cookie認証の有効化#546
Merged
Merged
Conversation
fix: enable CORS credentials and externalize frontend origin for Sanctum SPA auth
…tateful fix: include frontend origin in SANCTUM_STATEFUL_DOMAINS / Sanctumのstateful対象にフロントのオリジンを追加
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## feat/user #546 +/- ##
===============================================
+ Coverage 62.21% 62.56% +0.34%
- Complexity 1604 1620 +16
===============================================
Files 135 136 +1
Lines 8252 8339 +87
===============================================
+ Hits 5134 5217 +83
- Misses 3118 3122 +4 🚀 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 that Sanctum's cookie-based SPA authentication was broken in two independent ways:
config/cors.phphadsupports_credentials: false, so the browser discarded the session cookie on cross-origin responses even though it was issued successfully.config/sanctum.php'sstatefuldomain list did not include the frontend's origin (localhost:3876), so Sanctum never routed its requests through session-based auth and fell back to requiring a bearer token — which the frontend never sends (it is fully cookie-based, confirmed insrc/app/features/auth/apis/auth-api.ts).Both had to be fixed together for cookie-based authentication to work end-to-end. See #540 and #542 for the fuller investigation.
Closes #540
What I have done / 実施内容
supports_credentialstotrueinconfig/cors.php(with a comment explaining why Sanctum SPA auth requires it)allowed_originswithenv('FRONTEND_URL', 'http://localhost:3876')FRONTEND_URLto.env/.env.exampleFRONTEND_URLhost toconfig/sanctum.php'sstatefuldomains so Sanctum treats the frontend as a stateful (cookie-based) clientTest Results / テスト結果
docker exec heritage-app php artisan tinkerthatconfig('cors')resolvesallowed_originsto['http://localhost:3876', 'https://zigzagdev.github.io']withsupports_credentialstrue, andconfig('sanctum.stateful')includeslocalhost:3876LoginTest/LogoutTestpass with no regressions