Skip to content

Guard token storage, gate wallet connect on Freighter install, harden apiRequest - #306

Merged
chonilius merged 3 commits into
MergeFi:mainfrom
Temi-suwa18:fix/auth-token-guards-and-api-hardening
Aug 24, 2026
Merged

chonilius merged 3 commits into
MergeFi:mainfrom
Temi-suwa18:fix/auth-token-guards-and-api-hardening

Conversation

@Temi-suwa18

Copy link
Copy Markdown
Contributor

Summary

Four small, targeted bug fixes across lib/auth.ts, lib/wallet.ts, and lib/api.ts:

Unguarded token storage (#190) — setToken/clearToken/getToken in auth.ts called localStorage.setItem/removeItem/getItem with no try/catch, unlike ThemeContext.tsx#toggle, which already guards the identical class of failure for its own key. localStorage calls throw in real conditions (Safari private browsing, a full quota, a policy disabling storage entirely) — an unguarded setToken() during login throws synchronously, and if reads throw too, getToken() silently fails forever after, leaving the user stuck unable to sign in with no diagnosis of why. Fixed by wrapping all three in the same try/catch-and-degrade pattern already established in ThemeContext.tsx.

Dead installation check (#191) — isFreighterInstalled() was exported from wallet.ts but never called anywhere; connectWallet() went straight to isAllowed()/setAllowed()/getAddress() with no upfront "is the extension even installed" check, so a user without Freighter got whatever error those calls happened to throw instead of a clear message. Fixed by calling isFreighterInstalled() first in connectWallet() and short-circuiting with "Install the Freighter wallet extension to continue." before touching the rest of the flow.

No request timeout (#188) — apiRequest (used by apiPost, and directly by IssueActions/MilestoneActions for fund/claim/refund/deposit) called fetch() with no signal/AbortController/timeout. A stalled connection that never resolves left the await — and the surrounding finally { setPending(false) } — hanging forever, so an action button stayed on its pending label indefinitely with no way to retry short of reloading. Fixed by racing the fetch against a 20s AbortSignal.timeout() and throwing a clear "Request timed out — please try again." ApiRequestError, which the existing catch blocks already render.

No cache override (#189) — request() hardcoded cache: "no-store" with no per-call override, unconditionally opting every fetcher that funnels through it (fetchBounties, fetchMilestones, fetchMaintenancePools, fetchReputationByUsername) out of Next.js's fetch caching/ISR — even for pages like /issues, /milestones, /reputation/[handle] that aren't dashboards and could tolerate slightly stale data. Fixed by reordering so an explicit cache/next option in the caller's init now overrides the no-store default instead of always being clobbered by it.

Changes

  • src/lib/auth.ts — try/catch around all three localStorage calls.
  • src/lib/wallet.ts — connectWallet() checks isFreighterInstalled() up front.
  • src/lib/api.ts — apiRequest gets a 20s timeout via AbortSignal.timeout(); request()'s cache default is now overridable via init.

Test plan

  • npx tsc --noEmit — clean
  • npx eslint src/lib/auth.ts src/lib/wallet.ts src/lib/api.ts — clean
  • npx jest src/context/AuthContext.test.tsx src/context/WalletContext.test.tsx src/lib — 58/58 passing

Closes #190
Closes #191
Closes #188
Closes #189

setToken/clearToken/getToken in auth.ts wrote to and read from
localStorage with no try/catch, unlike the identical class of failure
ThemeContext.tsx already guards for its own key. localStorage.setItem/
removeItem/getItem throw in real browser conditions (Safari private
browsing, a full storage quota, or a policy that disables localStorage
entirely) — an unguarded setToken() during login throws synchronously,
and if reads throw too, getToken() silently fails on every subsequent
page load, leaving the user stuck unable to complete sign-in with no
diagnosis of why.

Wrapped all three in the same try/catch-and-degrade-gracefully pattern
already established in ThemeContext.tsx#toggle (closes MergeFi#190).
isFreighterInstalled() was exported from wallet.ts but never called
anywhere in the app — connectWallet() went straight to isAllowed()/
setAllowed()/getAddress() with no upfront "is the extension even
installed" check, so a user without Freighter got whatever error those
calls happened to throw instead of a clear, purpose-built message.

connectWallet() now short-circuits with "Install the Freighter wallet
extension to continue." when the extension isn't installed, before
touching isAllowed/getAddress, using the dedicated check this codebase
already wrote for exactly this (closes MergeFi#191).
apiRequest (used by apiPost, and directly by IssueActions/
MilestoneActions for fund/claim/refund/deposit) called fetch() with no
signal, AbortController, or timeout. If a request simply never
resolved — a stalled TCP connection, a backend that accepts the
connection but never responds — the await never settled, the
surrounding finally never ran, and the action button stayed on its
pending label ("Confirming in wallet...", "Claiming...") forever, with
no way to retry short of reloading. apiRequest now races the fetch
against a 20s AbortSignal.timeout() and throws a clear "Request timed
out — please try again." ApiRequestError, which the existing catch
blocks in IssueActions/MilestoneActions already know how to render
(closes MergeFi#188).

Separately, request() hardcoded `cache: "no-store"` with no per-call
override, unconditionally opting every fetcher that funnels through it
(fetchBounties, fetchMilestones, fetchMaintenancePools,
fetchReputationByUsername) out of Next.js's fetch caching/ISR — even
for /issues, /milestones, and /reputation/[handle], none of which are
dashboards, forcing a full backend round trip on every request.
Reordered so an explicit `cache`/`next` in the caller's init now
overrides the no-store default instead of always being clobbered by
it, so a call site can opt into `{ next: { revalidate: 30 } }` (closes
MergeFi#189).
@vercel

vercel Bot commented Aug 24, 2026

Copy link
Copy Markdown

@Temi-suwa18 is attempting to deploy a commit to the chonilius' projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave

drips-wave Bot commented Aug 24, 2026

Copy link
Copy Markdown

@Temi-suwa18 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! 🚀

Learn more about application limits

@chonilius
chonilius merged commit 1089773 into MergeFi:main Aug 24, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment