fix: repair broken record_winner API handler (undefined db + wrong signature)#9
Open
AliaksandrNazaruk wants to merge 1 commit into
Open
Conversation
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>
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.
Problem
The
record_winneraction insrc/routes/api/shots/+server.jsalways returns HTTP 500 — it can never record a winner. Two bugs:handleRecordWinner(request, data)(line 21), but the function was declaredhandleRecordWinner(winnerData)(line 161). SowinnerDatawas bound to the SvelteKitRequestobject, andwinnerData.winnerAddresswas alwaysundefined.db. The body calleddb.recordWinner(...), butdbis never imported in this module (it only importsverifyJWTSecure,getSupabaseJWTClient,isSupabaseServerAvailable).db.recordWinneris also a browser-only function (readslocalStorage), so it could never run server-side. Every call throwsReferenceError→ HTTP 500.By contrast, the sibling
record_shothandler works correctly.Fix
Rewrite
handleRecordWinnerto mirror the workinghandleRecordShot:(request, winnerData)signature.verifyJWTSecure; reject missing/invalid tokens (401).winnerData.winnerAddress(403 on mismatch).record_winner_secureRPC (defined insupabase/migrations/20250728150500_simplify_shot_recording.sql, alreadyGRANTed toauthenticated), called throughgetSupabaseJWTClient(token).Using the existing RPC (rather than a raw table insert) matches how
handleRecordShotrecords shots, and the RPC re-verifies the wallet address server-side (defense in depth) — so there's no RLS ambiguity. Nodbreference remains;node --checkpasses.Impact
Restores the winner-recording endpoint, which is currently 100% broken.