Skip to content

fix: repair broken record_winner API handler (undefined db + wrong signature)#9

Open
AliaksandrNazaruk wants to merge 1 commit into
profullstack:masterfrom
AliaksandrNazaruk:fix/record-winner-handler
Open

fix: repair broken record_winner API handler (undefined db + wrong signature)#9
AliaksandrNazaruk wants to merge 1 commit into
profullstack:masterfrom
AliaksandrNazaruk:fix/record-winner-handler

Conversation

@AliaksandrNazaruk

Copy link
Copy Markdown

Problem

The record_winner action in src/routes/api/shots/+server.js always returns HTTP 500 — it can never record a winner. Two bugs:

  1. Signature mismatch. The dispatcher calls handleRecordWinner(request, data) (line 21), but the function was declared handleRecordWinner(winnerData) (line 161). So winnerData was bound to the SvelteKit Request object, and winnerData.winnerAddress was always undefined.
  2. Undefined db. The body called db.recordWinner(...), but db is never imported in this module (it only imports verifyJWTSecure, getSupabaseJWTClient, isSupabaseServerAvailable). db.recordWinner is also a browser-only function (reads localStorage), so it could never run server-side. Every call throws ReferenceError → HTTP 500.

By contrast, the sibling record_shot handler works correctly.

Fix

Rewrite handleRecordWinner to mirror the working handleRecordShot:

  • Correct (request, winnerData) signature.
  • Verify the Bearer JWT via verifyJWTSecure; reject missing/invalid tokens (401).
  • Confirm the token's wallet matches winnerData.winnerAddress (403 on mismatch).
  • Insert via the existing record_winner_secure RPC (defined in supabase/migrations/20250728150500_simplify_shot_recording.sql, already GRANTed to authenticated), called through getSupabaseJWTClient(token).

Using the existing RPC (rather than a raw table insert) matches how handleRecordShot records shots, and the RPC re-verifies the wallet address server-side (defense in depth) — so there's no RLS ambiguity. No db reference remains; node --check passes.

Impact

Restores the winner-recording endpoint, which is currently 100% broken.

handleRecordWinner had two bugs that made the record_winner action
always return HTTP 500:

1. Signature mismatch: the dispatcher calls handleRecordWinner(request, data)
   but the function was declared handleRecordWinner(winnerData), so
   winnerData was bound to the Request object and winnerData.winnerAddress
   was always undefined.
2. Undefined 'db': the body called db.recordWinner(...), but db is never
   imported in this module (and db.recordWinner is a browser-only,
   localStorage-backed function that cannot run server-side), throwing a
   ReferenceError on every request.

Rewrite handleRecordWinner to mirror the working handleRecordShot:
correct (request, winnerData) signature, verify the Bearer JWT, confirm
the token wallet matches winnerAddress, then call the existing
record_winner_secure RPC (already GRANTed to authenticated and it
re-verifies the wallet server-side) via the JWT Supabase client.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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