Repository navigation
feat(auth): wallet sign-in and authFetch with single-flight refresh - #1355
Open
ValJnr-dev1 wants to merge 1 commit into
Open
ValJnr-dev1 wants to merge 1 commit into
ValJnr-dev1 wants to merge 1 commit into
Conversation
Implements CalloraOrg#1317. Changes: - src/services/authService.ts: signIn() uses requestAccess + signMessage (Freighter v6), builds an anti-replay message (origin, address, ISO timestamp, +5 min expiry, 16-byte nonce), POSTs to POST /api/auth/wallet. signOut() calls POST /api/auth/revoke. Access token kept in memory only; refresh token in sessionStorage (guarded with try/catch). - src/services/authFetch.ts: authFetch(path, init) prefixes with apiUrl(), injects Authorization: Bearer, refreshes once on 401 with a single shared promise so concurrent 401s never stampede. Abort signals cancel both attempts. Refresh failures clear the session and emit signalExpiry(). - src/App.tsx: WalletSignInButton added to the topbar (next to AccountSwitcher). AppSessionExpiryBanner wires useSessionExpiry() to SessionExpiryBanner inside the main content area. - src/pages/ApiUsage.tsx: usage fetch migrated from plain fetch() to authFetch() — the Authorization header is now sent. - docs/Configuration.md: new "Session model" section documents token storage trade-offs, sign-in sequence, refresh/retry flow, and the httpOnly-cookie upgrade path. - src/services/authService.test.ts: 25 tests covering all 9 issue scenarios (happy path, rejection, concurrent refresh, abort, localStorage isolation). - src/services/authFetch.test.ts: 14 tests. Coverage: authService 93.57% stmts / 83.56% branches / 100% funcs; authFetch 94.79% stmts / 92.30% branches / 83.33% funcs. All 2226 tests pass; typecheck clean.
|
@ValJnr-dev1 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Closes #1317
Summary
Implements wallet-based sign-in via Freighter and a single authenticated fetch helper (
authFetch) so the rest of the app has a standard way to callrequireAuthendpoints.Changes
src/services/authService.tssignIn(),signOut(),getAccessToken(),refreshAccessToken()src/services/authFetch.tsauthFetch(path, init)with single-flight refreshsrc/App.tsxWalletSignInButtonadded to topbar;AppSessionExpiryBannerwiredsrc/pages/ApiUsage.tsxfetch()toauthFetch()docs/Configuration.mdsrc/services/authService.test.tssrc/services/authFetch.test.tsSign-in sequence
Refresh and expiry
On a 401,
authFetchcallsrefreshAccessToken(). Concurrent 401s share a single refresh promise (no stampede). If the refresh also fails,signOut()clears local state andsignalExpiry()fires soSessionExpiryBannerappears. AnAbortSignalfrom the caller is forwarded to both the initial request and the retry.Token storage
sessionStorage, cleared when the tab closes.localStorageis never written to (verified by test Scenario 8).The docs section explains the trade-off and the path to upgrade to
httpOnlycookies once the backend addsSet-Cookie.Test output
Coverage for new services:
authService.ts: 93.57% stmts / 83.56% branches / 100% funcsauthFetch.ts: 94.79% stmts / 92.30% branches / 83.33% funcs