Skip to content

Auth: refresh an expired access token instead of failing every request - #41

Merged
jondeandres merged 1 commit into
mainfrom
refresh-expired-token
Nov 17, 2026
Merged

jondeandres merged 1 commit into
mainfrom
refresh-expired-token

Conversation

@jondeandres

Copy link
Copy Markdown
Contributor

Problem

After the app has been open for a while (or after the laptop wakes from sleep), every request fails with "API Error" until the app is reopened.

The frontend receives the access token once, from auto_login or sign-in, and never updates it. Supabase access tokens expire after an hour, so after that every REST call and every SSE stream is refused with a 401. Reopening the app works because auto_login refreshes the token at startup. The gRPC loop already refreshed its own token on Unauthenticated, but the new token never reached the frontend.

The message is a bare "API Error" because the 401 body has no error/message, and statusText is empty over HTTP/2.

Changes

  • refresh_session command (main.rs): wraps the existing refresh_access_token_into_state. The new tokens are stored in AppState and on disk, so the gRPC loop and the frontend always use the latest refresh token.
  • Refresh and retry on 401 (api.ts): on a 401, the client refreshes the token once and retries the request.
    • Requests refused at the same time share one refresh.
    • A request sent with a token that has since been replaced retries without refreshing again.
    • account_deleted still ends the session.
    • If the refresh fails, the original 401 is returned. The user is not signed out: the Rust side deliberately keeps the tokens when Supabase is unreachable.
  • Refresher wiring (AuthContext.tsx): Tauri mode uses refresh_session; web mode uses supabase.auth.refreshSession(). Demo mode is unchanged.
  • Streams reopen on a new token (useApiToken, FleetPage, PlaygroundPage): the fleet and models streams carry the token in their URL, so they reopen when it changes. On the fleet page, the once-a-minute membership check is what triggers the refresh. The playground's in-flight completion stream is no longer closed when the models stream reopens.

Testing

  • npm run test:unit: new src/lib/api.test.ts covers refresh and retry, the shared refresh, a token replaced while a request was in flight, a failed refresh, a refresh that still gets a 401, account_deleted, and token-change listeners.
  • npm test (Playwright): 95 passed. The suite runs in demo mode, so it does not exercise the real client.
  • cargo fmt --check, cargo clippy -D warnings and cargo test pass. refresh_session has no Rust test: it is a one-line wrapper over a function that calls Supabase and needs an AppHandle.
  • Not yet checked by hand: a release build left open for more than an hour, or run with a token that has already expired.

The desktop app handed the API client its access token once, at sign-in or
launch, and never again. Supabase tokens expire after an hour, so from then
on every request was refused with a 401 ("API Error") until the app was
reopened, which refreshes the token at startup.

- New `refresh_session` command exchanges the stored refresh token through
  the Rust host, which already owns the tokens.
- The API client refreshes once on a 401 and retries the request. Requests
  refused together share one refresh, and a deleted account still signs out.
- The fleet and models streams carry the token in their URL, so they reopen
  when it changes. A reply in progress in the playground keeps its stream.
@jondeandres
jondeandres merged commit 96cd0ba into main Nov 17, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant