-
Notifications
You must be signed in to change notification settings - Fork 6
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Problem
Auth routes manually instantiate UserService(db) in every endpoint handler:
# app/components/backend/api/auth/router.py — repeated in every route
async def register(user_data: UserCreate, db: AsyncSession = Depends(get_async_db)):
user_service = UserService(db)
...This creates boilerplate and bypasses FastAPI's dependency injection system. The service is stateful (holds a db session) but isn't wired as a proper dependency.
Proposed Change
Add a get_user_service dependency that composes get_async_db, so routes receive the service directly:
# app/components/backend/api/deps.py (or auth-specific deps)
async def get_user_service(
db: AsyncSession = Depends(get_async_db),
) -> UserService:
return UserService(db)Then routes simplify to:
@router.post("/register", response_model=UserResponse)
async def register(
user_data: UserCreate,
user_service: UserService = Depends(get_user_service),
):
existing_user = await user_service.get_user_by_email(user_data.email)
...Similarly, get_current_user_from_token in auth_service.py manually creates a UserService(db) — this should also use the injected service.
Files to Modify
app/components/backend/api/deps.py— addget_user_servicedependencyapp/components/backend/api/auth/router.py— replace manualUserService(db)withDepends(get_user_service)in all 8 route handlersapp/services/auth/auth_service.py— updateget_current_user_from_tokento acceptUserServiceinstead of rawdb
Benefits
- Eliminates repeated
UserService(db)boilerplate across 8 endpoints - Proper FastAPI DI — services are composable and testable via
app.dependency_overrides - Easier to mock in tests (override one dependency vs patching constructors)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request