Refresh access tokens in proxy instead of bouncing users to login#127
Merged
Conversation
The OAuth callback already sets a 30-day refresh_token cookie, but refreshAccessToken() in lib/idp/client.ts had no call sites — so when the short-lived access token expired, users were redirected to the IDP login page even though a valid refresh token was sitting in their cookies. The proxy now decodes the access token's exp claim, attempts a refresh via the refresh_token cookie when the access token is within 60s of expiry, and updates both cookies (preserving refresh token rotation if the IDP returns a new one). If refresh fails, the stale access token is cleared and the user is redirected to /login as before. Effective session lifetime goes from ~1 hour (access token TTL) to 30 days, sliding if the IDP rotates refresh tokens. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Forward the new access token to downstream handlers on the refresh
request itself via request.cookies.set + NextResponse.next({ request }).
Without this, Server Components rendered during the refreshing request
read the stale cookie and render as logged-out for one page load, even
though the browser gets the new cookie on the next request.
- Clear the refresh_token cookie on refresh failure. Leaving it set meant
every subsequent navigation re-triggered the same failing IDP round-trip.
- Validate that the refresh response actually contains a non-empty
access_token and expires_in; treat missing/empty as a refresh failure
rather than silently setting broken cookies.
- Log refresh failures via the existing logger.warn so IDP outages are
distinguishable from normal token expirations.
Also expand test coverage: assert cookie security attributes (httpOnly,
sameSite, path), verify the home-page redirect branch omits the redirect
query param, cover string/null exp claims in decodeJwtExp.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Summary
refresh_tokencookie, butrefreshAccessToken()inlib/idp/client.tshad zero call sites — so when the short-lived access token expired, users were redirected to the IDP login page even though a valid refresh token was sitting in their cookiesexpclaim, attempts a refresh via therefresh_tokencookie when within 60s of expiry, and updates both cookies (preserving refresh token rotation if the IDP returns a new one)/loginas beforeTest plan
npm run test— 183 passing, including 19 new testsnpm run build— Edge runtime bundle compiles cleanlytokencookie in DevTools, reload — confirm no login redirect and newtokencookie appearsrefresh_tokencookie to garbage, reload — confirm redirect to/login(not a loop)expires_in, reload — confirm no login redirect🤖 Generated with Claude Code