Repository navigation
Auth: refresh an expired access token instead of failing every request - #41
Merged
Merged
Conversation
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.
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.
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_loginor 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 becauseauto_loginrefreshes the token at startup. The gRPC loop already refreshed its own token onUnauthenticated, but the new token never reached the frontend.The message is a bare "API Error" because the 401 body has no
error/message, andstatusTextis empty over HTTP/2.Changes
refresh_sessioncommand (main.rs): wraps the existingrefresh_access_token_into_state. The new tokens are stored inAppStateand on disk, so the gRPC loop and the frontend always use the latest refresh token.api.ts): on a 401, the client refreshes the token once and retries the request.account_deletedstill ends the session.AuthContext.tsx): Tauri mode usesrefresh_session; web mode usessupabase.auth.refreshSession(). Demo mode is unchanged.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: newsrc/lib/api.test.tscovers 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 warningsandcargo testpass.refresh_sessionhas no Rust test: it is a one-line wrapper over a function that calls Supabase and needs anAppHandle.