""" 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