Reduce Supabase refresh-token churn (middleware + Realtime)#674
Draft
jon-bell wants to merge 3 commits into
Draft
Reduce Supabase refresh-token churn (middleware + Realtime)#674jon-bell wants to merge 3 commits into
jon-bell wants to merge 3 commits into
Conversation
- Read JWT from SSR auth cookies for middleware; call getSession only when missing, invalid, or expired, then forward Set-Cookie on the response. - Skip middleware for /api/* routes. - Sync Realtime JWT from onAuthStateChange; use getSession only to bootstrap when accessTokenValue is empty. - Add pure helpers and unit tests for JWT decode and cookie parsing. Co-authored-by: Jonathan Bell <jon@jonbell.net>
Contributor
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
The latest updates on your projects. Learn more about Argos notifications ↗︎
|
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.
Summary
Cuts unnecessary GoTrue refresh-token usage by avoiding
getSession()on every middleware invocation and by stopping periodic RealtimegetSession()polling when the browser client already refreshes the session.Changes
getSession()only when the token is missing, unreadable, lackssub, or is pastexp. Re-apply any refreshed cookies onto the finalNextResponse(including redirects)./api/*so API routes are not forced through session refresh /X-User-IDlogic.bindRealtimeAuth()subscribes toonAuthStateChangeand callsrealtime.setAuthonINITIAL_SESSION,SIGNED_IN, andTOKEN_REFRESHED. Channel subscribe/reconnect usesgetSession()only whenrealtime.accessTokenValueis still empty (bootstrap).Notes
X-User-IDand route gating; server layouts continue to usegetUser()for trusted checks where needed.getClaims()would callgetUser(); this path assumes asymmetric JWTs (typical for Supabase).Verification
npx jest tests/unit/supabase-jwt-payload.test.ts tests/unit/middleware-session.test.tsnpm run lint