Repository navigation
fix: exchange the Keystatic Cloud authorization code only once - #1674
Open
theluckystrike wants to merge 1 commit into
Open
theluckystrike wants to merge 1 commit into
theluckystrike wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: aa248ef The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This branch has not been deployed
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.
Fixes #1671
Signing in with Keystatic Cloud could end on
Bad response: 400 {"error":"invalid_grant","error_description":"Invalid code"}even though the sign-in had worked.The cause is in
KeystaticCloudAuthCallback. The token exchange runs in auseEffectthat depends onrouter, andRouterProviderbuilds a new router object on every render. Any re-render after the callback mounts runs the effect again with the same one-time code. The firstPOST /oauth/tokensucceeds and stores the token, the second is rejected withinvalid_grant, and itssetErrorreplaces the screen with the error.The fix keeps a
useRefflag that is set before the request, so the code is exchanged once per callback no matter how often the effect re-runs. The rest of the flow (stored state, token storage, redirect tofrom) is unchanged.Test:
packages/keystatic/src/app/cloud-auth-callback.test.tsxrenders the callback with a code and stored state, rerenders it with a fresh router object each time, and checks thatfetchis called once, the redirect happens, the token is stored and noinvalid_granterror is shown. Onmainit fails withexpected "vi.fn()" to be called 1 times, but got 4 times, with this change it passes.I ran
npx vitest run --project keystatic(59 files, 708 passed, 8 skipped),npx tsc, and eslint and prettier on the changed files. I added a patch changeset for@keystatic/core.