fix(frontend): refresh SEP-10 JWT before expiry to prevent stale-token 401s#588
Open
opepraise wants to merge 1 commit into
Open
fix(frontend): refresh SEP-10 JWT before expiry to prevent stale-token 401s#588opepraise wants to merge 1 commit into
opepraise wants to merge 1 commit into
Conversation
…n 401s Closes astera-hq#576 Long-lived sessions (e.g. admin monitoring tabs open for >24 h) silently failed after the JWT expired because there was no refresh mechanism. - Add `getTokenExpiry`, `isTokenExpired`, `isTokenExpiringSoon` helpers to decode the JWT exp claim client-side without signature verification. - Add `authenticatedFetch` wrapper that proactively refreshes the token (5 min before expiry) and retries once on a 401 via the SEP-10 challenge/response flow. Returns the 401 as-is if re-auth fails so callers can redirect to the connect-wallet flow. - Add `useAuthRefresh` hook that schedules a silent background refresh timed to the stored token's expiry; reschedules itself after success. - Add 13 Jest tests covering the helpers and the 401-retry path.
Contributor
|
@opepraise Thanks for this, the overall approach is right and the code is clean. A few things before I can merge: Blocker
Please address before merge
Nice to have (can be a follow-up)
The structure, the |
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
Fixes #576
Long-lived sessions (admin monitoring tabs open for >24h) silently failed after the 24h JWT expired because there was no refresh mechanism. API calls returned 401 but the UI showed no error and no re-auth prompt, making users believe transactions submitted successfully when they were actually rejected.
lib/auth.ts— AddgetTokenExpiry,isTokenExpired,isTokenExpiringSoonhelpers that decode the JWTexpclaim client-side (no signature verification needed — the server verifies on every call). AddauthenticatedFetchwrapper that proactively refreshes 5 min before expiry and retries once on a 401 via the SEP-10 challenge/response flow; returns the 401 as-is if re-auth fails so callers can redirect to the connect-wallet flow.lib/hooks.ts— AdduseAuthRefreshhook that schedules a silent background refresh timed to the stored token's expiry and reschedules itself after each successful refresh.__tests__/lib/auth.test.ts— 13 Jest tests covering token expiry helpers and the full 401-retry path (valid token → server 401 → re-auth → retry → 200).Test plan
getTokenExpiry— parses exp from well-formed JWT, returns null for malformed/missing expisTokenExpired— true for past exp, false for future exp, true for malformedisTokenExpiringSoon— true within 5-min margin, false beyond marginauthenticatedFetch— attaches Bearer header, retries on 401 with fresh token, returns 401 when re-auth fails