"""FastAPI dependencies for authentication 供其他模組引用的 dependency injection 函數 """ from fastapi import Request, HTTPException, status async def get_current_user(request: Request) -> dict: """Get current authenticated user from request state Usage in other modules: from app.modules.auth import get_current_user @router.get("/my-endpoint") async def my_endpoint(current_user: dict = Depends(get_current_user)): username = current_user["username"] display_name = current_user["display_name"] ... Returns: dict: {"id": int, "username": str, "display_name": str} Raises: HTTPException: If user not authenticated (middleware should prevent this) """ if not hasattr(request.state, "user"): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Authentication required" ) return request.state.user