Spaces:
No application file
No application file
| """ | |
| API dependencies for request handling. | |
| Used for dependency injection in FastAPI endpoints. | |
| """ | |
| from fastapi import Request, HTTPException, status | |
| def get_current_user_id(request: Request) -> str: | |
| """ | |
| Extract authenticated user ID from request state. | |
| This dependency requires the JWT middleware to have run first, | |
| which attaches user_id to request.state after token verification. | |
| Args: | |
| request: FastAPI request object | |
| Returns: | |
| User ID string from JWT token | |
| Raises: | |
| HTTPException: 401 if user_id not found in request state | |
| """ | |
| if not hasattr(request.state, "user_id"): | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="User not authenticated" | |
| ) | |
| return request.state.user_id | |