feat: implement silent access token refresh on 401#39
Merged
Conversation
Contributor
|
@libby-coder resolve conflicts |
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
Closes #24
Implements silent access token refresh so expired tokens are transparently renewed without forcing the user to log in again.
src/lib/auth/token-store.ts— centralised read/write/clear for both access token and refresh token. Access token is mirrored to a cookie (for Next.js middleware); refresh token stays inlocalStorageonly.src/lib/auth/refresh.ts— single-flight refresh logic. Concurrent 401s queue behind one/auth/refreshcall and all receive the same new token on success. On failure, all tokens are cleared and the user is redirected to/login.src/lib/api/client.ts— 401 interceptor: oncode: ACCESS_TOKEN_EXPIRED, callsrefreshAccessToken()then retries the original request once (_retryflag prevents infinite loops).src/lib/api/auth.ts—login()andsignup()now persist the refresh token returned by the backend;logout()clears both tokens.vitest.config.ts+ unit tests — 24 tests covering queue behaviour (parallel 401s → single fetch), token rotation, and the full refresh-failure path (clear + redirect).README.md— documents the cookie vs localStorage strategy and the silent refresh flow.Acceptance criteria
/login_retryflag)NODE_ENVguard)Test plan
npm test— all 24 unit tests should pass/login