Skip to content

fix: browse and search routes add request details only for a checked account - #489

Merged
kikootwo merged 1 commit into
mainfrom
fix/discovery-request-details
Oct 27, 2026
Merged

kikootwo merged 1 commit into
mainfrom
fix/discovery-request-details

Conversation

@kikootwo

Copy link
Copy Markdown
Owner

What changed

  • New optional-auth helper getOptionalAuthUser next to requireAuth (src/lib/middleware/auth.ts). It runs the same account checks as requireAuth and takes the role from the account row. It returns the caller, or null; it never answers 401. Both helpers share one access-token check (checkAccessToken), so the refusal rules live in one place. An API token counts only on an endpoint on the API-token allowlist, as with requireAuth.
  • These routes still answer without a session and now take the caller from getOptionalAuthUser:
    • src/app/api/audiobooks/popular/route.ts (GET)
    • src/app/api/audiobooks/new-releases/route.ts (GET)
    • src/app/api/audiobooks/category/[categoryId]/route.ts (GET)
    • src/app/api/audiobooks/search/route.ts (GET)
  • enrichAudiobooksWithMatches (src/lib/utils/audiobook-matcher.ts) adds request details (isRequested, requestStatus, requestId, requestedByUserId, requestedByUsername) only when it gets a caller id, and skips the request lookup without one. Without a checked caller every book reports isRequested: false and null request fields, and isIgnored: false. Availability and reported-issue flags are unchanged for everyone.
  • Removed getCurrentUser, getCurrentUserAsync and isAdmin from src/lib/middleware/auth.ts. They have no callers left.
  • Docs: documentation/backend/services/auth.md (middleware list and one Fixed Issues line) and documentation/backend/services/api-tokens.md (search route and helper names).

Why

These routes now check the account before adding request details, like the other signed-in routes.

Evidence

  • New tests/api/discovery-routes.request-details.test.ts runs the real auth middleware and the real matcher against all four handlers. On each route, the following get 200 with the same books and no request details: a deleted account, a missing account row, a pending account, a rejected account, a token issued before the sessions were revoked, no token, and an unverifiable token. A live account gets exactly the body it got before, requester names included. On search, an API token gets the same body as before. An API token whose owner was deleted gets no request details, and so does an API token on the three routes that are not on the allowlist.
  • New case in tests/utils/audiobook-matcher.test.ts: without a caller id, no request details and no request lookup.
  • On main (5cf6f11) with these test files: 33 failed, 10 passed, all on an assertion. The 10 that pass on main are the 4 live-account cases, the API-token search case and the 5 existing matcher cases, which confirms the signed-in bodies are unchanged. On this branch: 43/43 pass.
  • tests/middleware/auth.middleware.test.ts, session-account-status.test.ts and session-current-role.test.ts now test getOptionalAuthUser in place of the removed helpers (JWT account row, current role, allowlist, API-token account status). tests/api/audiobooks-browse.routes.test.ts mocks the new helper.
  • Pages: home, search and category sections fetch these routes through authenticatedFetcher inside ProtectedRoute, after the session has loaded, so a signed-in user sees the same badges and requester names as before.

Checks

  • Unit tests for the changed code (vitest related on the 6 source files plus the 7 test files): PASS, 123 files, 1583/1583
  • Type check and lint: PASS (tsc --noEmit clean; ESLint on the 13 touched files: 43 findings on main and 43 on this branch, same rules per file, none new)
  • Image build + smoke (npm run e2e -- --specs smoke, image rmab-e2e/t106:83c4ae8, same source as the head commit): PASS, 1/1
  • Full unit suite: PR Tests (CI)

Risk and rollback

  • A signed-in request to these routes now makes one user lookup, as every route behind requireAuth does. A request with no checked caller skips the request lookup.
  • Rollback: revert this PR. No data or schema changes.

Release note

  • Browse and search results show request details only to signed-in accounts that pass the usual account checks.

🤖 Generated with Claude Code

Popular, new releases, category and audiobook search take the caller from a
new optional-auth helper, getOptionalAuthUser, which runs the same account
checks as requireAuth and returns null instead of a 401. Request details and
ignore flags are added only for a checked account; any other caller gets the
same books without them. Signed-in responses are unchanged. The unused
getCurrentUser, getCurrentUserAsync and isAdmin helpers are removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kikootwo
kikootwo merged commit d8011ab into main Oct 27, 2026
1 check passed
@kikootwo
kikootwo deleted the fix/discovery-request-details branch October 27, 2026 10:06
@kikootwo kikootwo mentioned this pull request Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant