fix: recover cleanly when the stored session is dead#72
Merged
Conversation
Companion to devicecloud-dev/dcd#1084 (which fixes the root cause of "Invalid Refresh Token: Already Used" by minting the CLI its own session at login). This fixes the recovery path for sessions that are already broken: - CliAuthGateway.refresh now throws a typed SessionRefreshError whose `definitive` flag distinguishes a GoTrue 4xx token rejection from transient failures (network, 5xx). - On a definitive rejection, resolveAuth drops the dead session from the config (keeping env/api_url/org), so subsequent commands report a clean "Not authenticated" instead of failing the same refresh every time. - `dcd login` no longer dead-ends users whose session has expired: the "Already logged in… Sign out and log in again?" confirm is skipped for expired sessions (it proceeds straight to sign-in), and defaults to Yes for live ones — running `dcd login` deliberately almost always means "log me in". Also updates the login-flow comments and CLAUDE.md for the new handoff contract (access token as identity proof; api mints a dedicated session). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
finalerock44
added a commit
that referenced
this pull request
Jul 10, 2026
Promote from dev to stable: * fix: recover cleanly when the stored session is dead (#72) — `dcd login` now mints a dedicated Supabase session for the CLI rather than reusing the browser's refresh-token family, and a dead stored session recovers instead of hard-failing. * deps: bump the minor-and-patch group (9 updates) (#71) and eslint-plugin-unicorn 68 -> 69 (#70). Carries only the source delta — package.json version, CHANGELOG.md and the release-please manifests stay as release-please left them on production. Release-As: 5.1.1
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.
Companion to devicecloud-dev/dcd#1084, which fixed the root cause of
Invalid Refresh Token: Already Used(the login handoff put the CLI in the browser's refresh-token family). This PR fixes the CLI's recovery path for sessions that are already broken — including every session minted before that fix deployed.Before
From the customer report: a run fails with
Failed to refresh session: Invalid Refresh Token: Already Used. Rundcd loginagain.— butdcd loginthen saysAlready logged in as … Sign out and log in again?defaulting to No, so pressing enter keeps the dead session. Every subsequent command fails with the same refresh error.After
CliAuthGateway.refreshthrows a typedSessionRefreshErrorwhosedefinitiveflag distinguishes a GoTrue 4xx token rejection (revoked / already used — only a re-login fixes it) from transient failures (network, 5xx) where the token may still be good.resolveAuthdrops the dead session from the config while keepingenv/api_url/org, so the next command reports a cleanNot authenticated… run dcd loginanddcd logingoes straight in with no prompt.dcd loginskips the "Already logged in" confirm entirely when the stored session is expired (prints "Your session for has expired — signing in again." and proceeds), and the confirm for a live session now defaults to Yes — runningdcd logindeliberately almost always means "log me in".Also refreshes the login-flow comments and CLAUDE.md for the new handoff contract (access token as identity proof; the api mints the CLI a dedicated session).
Testing
pnpm lint/pnpm typecheckclean; fullpnpm testsuite passes (154 tests against the mock API).dropStoredSessionremoves only the session (idempotent, keeps env/org),SessionRefreshErrorflag semantics.DCD_CONFIG_DIR:dcd listfails with the refresh error and drops the session (env/org preserved);dcd list→ cleanNot authenticatedmessage;dcd login→ no dead-end prompt, straight to the sign-in URL.🤖 Generated with Claude Code
Need help on this PR? Tag
/codesmithwith what you need. Autofix is disabled.