Hussien Haider commited on
Commit
6878a15
·
1 Parent(s): b98d40f
Files changed (2) hide show
  1. app/api/onevone.py +7 -4
  2. app/core/auth.py +1 -0
app/api/onevone.py CHANGED
@@ -123,8 +123,10 @@ async def _sb_select(table: str, query: str) -> list[dict]:
123
 
124
  async def _sb_insert(table: str, row: dict) -> dict:
125
  url = f"{SUPABASE_URL}/rest/v1/{table}"
 
126
  async with httpx.AsyncClient(timeout=20) as client:
127
  r = await client.post(url, json=row, headers=_sb_headers(content_type=True, prefer="return=representation"))
 
128
  if r.status_code not in (200, 201):
129
  raise HTTPException(status_code=502, detail=f"Supabase insert error ({table}): {r.text[:200]}")
130
  rows = r.json()
@@ -342,16 +344,15 @@ class SubmitRequest(BaseModel):
342
 
343
  @router.post("/rooms")
344
  async def create_room(req: CreateRoomRequest, user: dict = Depends(get_current_user)):
345
- # IDOR FIX: Use authenticated user's ID
346
  user_id = user["user_id"]
 
347
  if req.teamRole not in ("red", "blue"):
348
  raise HTTPException(status_code=400, detail="teamRole يجب أن يكون red أو blue")
349
  if req.mainDurationS not in MAIN_DURATION_OPTIONS:
350
- raise HTTPException(status_code=400, detail="mainDurationS غير صالح")
351
  if not req.displayName:
352
  raise HTTPException(status_code=400, detail="بيانات المستخدم ناقصة")
353
 
354
- # generate a unique 6-char code (retry on collision)
355
  code = _gen_room_code()
356
  for _ in range(8):
357
  existing = await _sb_select("onevone_rooms", f"code=eq.{code}&select=id&limit=1")
@@ -359,6 +360,7 @@ async def create_room(req: CreateRoomRequest, user: dict = Depends(get_current_u
359
  break
360
  code = _gen_room_code()
361
 
 
362
  room = await _sb_insert("onevone_rooms", {
363
  "code": code,
364
  "owner_user_id": user_id,
@@ -367,7 +369,7 @@ async def create_room(req: CreateRoomRequest, user: dict = Depends(get_current_u
367
  "challenge_source": req.challengeSource,
368
  "main_duration_s": req.mainDurationS,
369
  })
370
- # add the owner as slot 1
371
  await _sb_insert("onevone_players", {
372
  "room_id": room["id"],
373
  "user_id": user_id,
@@ -375,6 +377,7 @@ async def create_room(req: CreateRoomRequest, user: dict = Depends(get_current_u
375
  "display_name": req.displayName,
376
  "is_ready": True,
377
  })
 
378
  return {"room": room, "code": code}
379
 
380
 
 
123
 
124
  async def _sb_insert(table: str, row: dict) -> dict:
125
  url = f"{SUPABASE_URL}/rest/v1/{table}"
126
+ print(f"[onevone] _sb_insert: table={table}, row={json.dumps(row, default=str)}")
127
  async with httpx.AsyncClient(timeout=20) as client:
128
  r = await client.post(url, json=row, headers=_sb_headers(content_type=True, prefer="return=representation"))
129
+ print(f"[onevone] _sb_insert: status={r.status_code}, body={r.text[:500]}")
130
  if r.status_code not in (200, 201):
131
  raise HTTPException(status_code=502, detail=f"Supabase insert error ({table}): {r.text[:200]}")
132
  rows = r.json()
 
344
 
345
  @router.post("/rooms")
346
  async def create_room(req: CreateRoomRequest, user: dict = Depends(get_current_user)):
 
347
  user_id = user["user_id"]
348
+ print(f"[onevone] create_room: user_id={user_id}, teamRole={req.teamRole}, mainDurationS={req.mainDurationS}")
349
  if req.teamRole not in ("red", "blue"):
350
  raise HTTPException(status_code=400, detail="teamRole يجب أن يكون red أو blue")
351
  if req.mainDurationS not in MAIN_DURATION_OPTIONS:
352
+ raise HTTPException(status_code=400, detail=f"mainDurationS {req.mainDurationS} غير صالح، الخيارات: {MAIN_DURATION_OPTIONS}")
353
  if not req.displayName:
354
  raise HTTPException(status_code=400, detail="بيانات المستخدم ناقصة")
355
 
 
356
  code = _gen_room_code()
357
  for _ in range(8):
358
  existing = await _sb_select("onevone_rooms", f"code=eq.{code}&select=id&limit=1")
 
360
  break
361
  code = _gen_room_code()
362
 
363
+ print(f"[onevone] create_room: inserting room with code={code}")
364
  room = await _sb_insert("onevone_rooms", {
365
  "code": code,
366
  "owner_user_id": user_id,
 
369
  "challenge_source": req.challengeSource,
370
  "main_duration_s": req.mainDurationS,
371
  })
372
+ print(f"[onevone] create_room: room inserted id={room.get('id')}, inserting player slot=1")
373
  await _sb_insert("onevone_players", {
374
  "room_id": room["id"],
375
  "user_id": user_id,
 
377
  "display_name": req.displayName,
378
  "is_ready": True,
379
  })
380
+ print(f"[onevone] create_room: done, returning code={code}")
381
  return {"room": room, "code": code}
382
 
383
 
app/core/auth.py CHANGED
@@ -87,6 +87,7 @@ async def get_current_user(request: Request) -> dict:
87
  import json
88
  body = json.loads(body_bytes)
89
  user_id = body.get("user_id", "") or body.get("userId", "")
 
90
  if user_id:
91
  log.info("[auth] Body authenticated, user_id=%s", user_id)
92
  return {"user_id": user_id, "email": "", "role": "authenticated"}
 
87
  import json
88
  body = json.loads(body_bytes)
89
  user_id = body.get("user_id", "") or body.get("userId", "")
90
+ print(f"[auth] POST body keys: {list(body.keys())}, found user_id={user_id!r}")
91
  if user_id:
92
  log.info("[auth] Body authenticated, user_id=%s", user_id)
93
  return {"user_id": user_id, "email": "", "role": "authenticated"}