strat-gameplay-webapp/backend/app/api/dependencies.py
Cal Corum 38fb76c849 CLAUDE: Fix resolution phase control and add demo mode
Bug fix: During resolution phase (dice rolling), isMyTurn was false
for both players, preventing anyone from seeing the dice roller.
Now the batting team has control during resolution since they read
their card.

Demo mode: myTeamId now returns whichever team needs to act,
allowing single-player testing of both sides.

Changes:
- Add creator_discord_id to GameState (backend + frontend types)
- Add get_current_user_optional dependency for optional auth
- Update quick-create to capture creator's discord_id
- Fix isMyTurn to give batting team control during resolution
- Demo mode: myTeamId returns active team based on phase

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-13 23:47:21 -06:00

50 lines
1.4 KiB
Python

"""
FastAPI dependencies for authentication and database access.
These dependencies can be injected into route handlers using FastAPI's Depends() mechanism.
"""
import logging
from fastapi import Request
from jose import JWTError
from app.utils.auth import verify_token
from app.utils.cookies import ACCESS_TOKEN_COOKIE
logger = logging.getLogger(f"{__name__}.dependencies")
async def get_current_user_optional(request: Request) -> dict | None:
"""
Get current authenticated user from cookie (optional).
This dependency returns the user dict if authenticated, or None if not.
Use this when authentication is optional but you want to capture user info
if available (e.g., for tracking game creators).
Args:
request: FastAPI request object
Returns:
User dict with discord_id, user_id, username if authenticated, None otherwise
"""
token = request.cookies.get(ACCESS_TOKEN_COOKIE)
if not token:
return None
try:
payload = verify_token(token)
return {
"discord_id": payload.get("discord_id"),
"user_id": payload.get("user_id"),
"username": payload.get("username"),
}
except JWTError:
logger.debug("Invalid token in optional auth check")
return None
except Exception as e:
logger.debug(f"Error verifying token in optional auth: {e}")
return None