Repository navigation
auth: handle rejected API tokens in AccountsStore [2/10] - #22937
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The stale-token mismatch safety path needs regression coverage before this authentication refactor is approved.
Review effort: Balanced
Findings: 1
What changed in this PR
Moves rejected-token handling into AccountsStore, preparing authentication lifecycle ownership for token renewal.
Changes:
- Centralizes account removal, token revocation, and invalidation events.
- Updates API subscription and recovery-dialog behavior.
- Adds integration, error-path, and UI tests.
| File | Description |
|---|---|
app/src/lib/api.ts |
Adds token-listener unsubscription. |
app/src/lib/stores/accounts-store.ts |
Handles rejected credentials and emits invalidation events. |
app/src/lib/stores/app-store.ts |
Shows the invalid-token dialog from store events. |
app/src/ui/index.tsx |
Connects API invalidations to AccountsStore. |
app/src/ui/invalidated-token/invalidated-token.tsx |
Narrows dispatcher dependencies and updates wording. |
app/test/unit/account-token-lifecycle-test.ts |
Tests callback error reporting. |
app/test/unit/oauth-authentication-test.ts |
Tests API-to-store invalidation routing. |
app/test/unit/ui/invalidated-token-test.tsx |
Tests recovery-dialog actions. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The ownership transfer is coherent, preserves existing safeguards, and includes focused coverage across the API, store, and UI layers.
Review effort: Balanced
Findings: None
Resolved since last review (1)
niik
left a comment
There was a problem hiding this comment.
This ownership move looks coherent and preserves the existing stale-token safeguard while putting the decision in the store that will own credential renewal. The API routing, proxy/OTP exclusions, and recovery-dialog behavior are well covered.
The keychain-removal and concurrent-invalidation weaknesses appear inherited and are expected to be addressed by the coordinated credential lifecycle in #22939, so I do not consider them blocking for this refactor layer.
One non-blocking follow-up: this path now calls AccountsStore.removeAccount directly and no longer produces the successful account-removal log previously emitted by AppStore._removeAccount. Please restore an informational log when rejected credentials cause an automatic sign-out so unexpected account removal remains diagnosable from user logs.
AppStore used to subscribe to API.onTokenInvalidated directly, decide whether the rejected token still belonged to the account, sign the account out, and show the "token invalidated" dialog. Credential state lives in AccountsStore, and upcoming work makes the answer to "is this still the account's token?" depend on credential renewal that only AccountsStore can see. Move the decision there first, as a refactor with development's behavior. AccountsStore now: - exposes `handleTokenInvalidated(endpoint, token)`, the callback index.tsx registers with `API.onTokenInvalidated`. It runs `invalidateToken` and reports failures through the store's error event instead of leaving an unhandled rejection; - implements `invalidateToken` with the logic AppStore had: ignore endpoints without an account, ignore (and log) a rejected token that doesn't match the account's current token, otherwise remove the account and revoke the rejected token remotely; - emits `token-invalidated` with the signed-out account (its token cleared) through a new `onTokenInvalidated` subscription. AppStore only listens for that event and shows the InvalidatedToken popup, and no longer touches the API's static listener. `API.onTokenInvalidated` now returns a function that removes the listener, so tests (and later callers) can unsubscribe. The InvalidatedToken dialog takes only the two dispatcher methods it calls, which lets tests render it with a small fake. Its copy now says the token "is no longer valid" rather than "has been invalidated": a token can stop working because it expired, not only because it was revoked. Tests cover routing a GitHub 401 through API into AccountsStore, ignoring proxy and OTP-required 401s, reporting invalidation failures through the store, and the dialog's sign-in and dismiss actions. Co-authored-by: Copilot <[email protected]>
da72e93 to
3940a8a
Compare
|
Restored log in #22959, thanks! 🙏 |
On `development`, a 401 for an account's current token went through `AppStore._removeAccount`, which logs `[AppStore] removing account <login> (<name>) from store` before removing it. With credential handling moved into `AccountsStore` and `CredentialSessions`, automatic sign-outs no longer pass through `AppStore._removeAccount`, so nothing in the user's log said which account was removed or that it was removed at all. That makes "why was I signed out?" reports impossible to diagnose from logs. All automatic sign-outs funnel through `AccountsStore.requireSignIn`: - a 401 for the account's current access token, - a refresh token the server rejects, or a renewal response that cannot be used, - a renewed credential that cannot be saved to secure storage. `requireSignIn` now logs, at info level and only after the account was actually retired (so stale or duplicate requests stay silent): [AccountsStore] signing out account <login> (<name>) because its credentials can no longer be used It carries the same identifying details `development` already logged and never any token. The specific cause is logged by the caller (renewal failure, save failure), so this line ties those to the account that was signed out. User-initiated sign-out still logs through `AppStore._removeAccount` as before. No test is added: this only adds a log line. Addresses non-blocking review feedback from @niik on #22937. Co-authored-by: Copilot <[email protected]>
On `development`, a 401 for an account's current token went through `AppStore._removeAccount`, which logs `[AppStore] removing account <login> (<name>) from store` before removing it. With credential handling moved into `AccountsStore` and `CredentialSessions`, automatic sign-outs no longer pass through `AppStore._removeAccount`, so nothing in the user's log said which account was removed or that it was removed at all. That makes "why was I signed out?" reports impossible to diagnose from logs. All automatic sign-outs funnel through `AccountsStore.requireSignIn`: - a 401 for the account's current access token, - a refresh token the server rejects, or a renewal response that cannot be used, - a renewed credential that cannot be saved to secure storage. `requireSignIn` now logs, at info level and only after the account was actually retired (so stale or duplicate requests stay silent): [AccountsStore] signing out account <login> (<name>) because its credentials can no longer be used It carries the same identifying details `development` already logged and never any token. The specific cause is logged by the caller (renewal failure, save failure), so this line ties those to the account that was signed out. User-initiated sign-out still logs through `AppStore._removeAccount` as before. No test is added: this only adds a log line. Addresses non-blocking review feedback from @niik on #22937. Co-authored-by: Copilot <[email protected]>
Brings in short-lived token support (#22917, #22937, #22938, #22939) and adapts it to multiple accounts per endpoint: - Credential sessions are keyed per account (endpoint + login) instead of per endpoint, so renewal, invalidation and storage writes are isolated. - AccountsStore lookups, retirement and refresh match the exact account. - The invalidated token dialog names the signed-out login and offers a targeted sign-in for that account. Co-authored-by: Copilot App <[email protected]>

Description
Moves the handling of a GitHub 401 ("token rejected") from AppStore to AccountsStore, keeping development's behavior. Later PRs make "is this still the account's token?" depend on token renewal that only AccountsStore can see, so the decision has to live there.
handleTokenInvalidated, the callback index.tsx registers withAPI.onTokenInvalidated, and emitstoken-invalidatedwith the signed-out account. Failures are reported through the store's error event.API.onTokenInvalidatedreturns an unsubscribe function.Stack
Merge in order, starting with 1. Each PR builds, passes tests, and keeps production behavior on its own.
Validation
git/push, 1 barerev-parse).Release notes
Notes: no-notes