brief-rags-bench/app/api/v1/auth.py

54 lines
1.4 KiB
Python

"""Authentication API endpoints."""
from fastapi import APIRouter, Depends, HTTPException, Request, status
from app.dependencies import get_db_client
from app.interfaces.db_api_client import DBApiClient
from app.models.auth import LoginResponse
from app.services.auth_service import AuthService
router = APIRouter(prefix="/auth", tags=["auth"])
@router.post("/login", response_model=LoginResponse)
async def login(
request: Request,
login: str,
db_client: DBApiClient = Depends(get_db_client)
):
"""
Authenticate user with 8-digit login.
Args:
request: FastAPI request object (to get client IP)
login: 8-digit login string
db_client: DB API client instance
Returns:
LoginResponse with JWT token and user info
Raises:
HTTPException 400: If login format is invalid
HTTPException 500: If DB API call fails
"""
client_ip = request.client.host
auth_service = AuthService(db_client)
try:
response = await auth_service.login(login, client_ip)
return response
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e)
)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Authentication failed: {str(e)}"
)