fix: guard apiFetch with Keycloak ready + auth check #162
No reviewers
Labels
No labels
domain:backend
domain:devops
domain:frontend
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
forgejo_admin/westside-landing!162
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "153-fix-auth-guard-race"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Protected pages fire API calls in
onMountbefore Keycloak init completes, causing 401 console errors on unauthenticated access. This fix makesapiFetchandapiUploadauth-aware by awaiting the Keycloakreadypromise and returningnullearly if not authenticated.Changes
src/lib/api.js— ImportreadyandisAuthenticatedfromkeycloak.js. Addawait ready; if (!isAuthenticated()) return null;guard at the top of bothapiFetchandapiUpload.Test Plan
/my-players,/admin,/coach) while unauthenticated/signinshould still work as beforeapiUploadflows (e.g. photo upload) still work when authenticatedReview Checklist
npm run checkpasses (0 errors, 7 pre-existing warnings)src/lib/api.jsRelated
Related Notes
N/A — no pal-e-docs notes affected.
QA Review -- PR #162
Diff Summary
Single file changed:
src/lib/api.js(+7, -1). Adds auth guards toapiFetchandapiUpload.Findings
Correctness
readyis a module-levelPromiseexported fromkeycloak.js(line 34). Awaiting it guarantees Keycloak init has completed before any API call proceeds. Correct.isAuthenticated()checkskeycloak?.authenticated ?? false(line 121). Returnsfalsebefore init and when unauthenticated. Correct.nullearly is safe. All 8 protected page callers wrapapiFetchin try/catch. Anullreturn does not throw, so it falls through to thefinallyblock (setsloading = false). The auth guard$effectin(app)/+layout.sveltethen redirects to/signin.No regressions
await readyresolves immediately (already resolved),isAuthenticated()returnstrue, execution continues togetToken()as before. No behavior change.apiUploadgets the same treatment. Symmetric fix.SOP compliance
153-fix-auth-guard-race-- matches{issue}-{kebab}convention.svelte-checkpasses: 0 errors (7 pre-existing a11y warnings, unrelated).Closes #153, test plan, review checklist. Complete.Nits
VERDICT: APPROVED