fix(cli): skip GoTrue call when access token is still valid#2497
Open
fix(cli): skip GoTrue call when access token is still valid#2497
Conversation
getRequestAuthHeaders() called supabase.auth.setSession() on every CLI request, hitting GoTrue even when the local JWT was still valid. This added unnecessary load to the auth service. The fix decodes the JWT locally and only calls setSession() when the token is expired or about to expire (within 60s). Valid tokens are returned directly as Bearer tokens. Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
🧪 BenchmarkShould we run the Virtual MCP strategy benchmark for this PR? React with 👍 to run the benchmark.
Benchmark will run on the next push after you react. |
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.
Context
This is the companion fix to deco-sites/admin#2904, which addresses a Supabase GoTrue CPU spike that has been recurring since Feb 22.
Problem
getRequestAuthHeaders()in the CLI callssupabase.auth.setSession()on every CLI request, which sends a POST to Supabase GoTrue to validate/exchange the refresh token. This happens even when the local access token JWT is still valid and has minutes or hours until expiry.With the 24-hour JWT expiry configured on the
decocmsSupabase project (jwt_exp: 86400), the vast majority of CLI calls within a session have a perfectly valid access token. Yet every call still hits GoTrue, adding unnecessary load to the auth service.Fix
Before calling
setSession(), decode the JWT locally and check itsexpclaim. If the token has more than 60 seconds until expiry, return it directly as a Bearer token without hitting GoTrue.The 60-second buffer ensures tokens are refreshed well before they expire, avoiding edge cases where a token expires mid-request.
Before:
After:
Impact
jose.decodeJwtis already imported and used inreadSession(), so no new dependenciessetSession()is preserved for expired/expiring tokens and decode failuresTest plan
deco auth loginstill works and stores tokensdeco auth whoamiworks without extra network callsMade with Cursor
Summary by cubic
Skip GoTrue calls in CLI when the access token is still valid, returning the JWT directly as a Bearer token. This cuts most unnecessary auth requests and reduces load on Supabase GoTrue.
Written for commit fd7fb9d. Summary will update on new commits.