This commit is contained in:
@@ -247,19 +247,55 @@ async def register_user(user_create: UserCreate, request: Request):
|
||||
@app.post("/api/v1/auth/login", response_model=Token, tags=["Authentication"], summary="Login user")
|
||||
async def login_user(user_login: UserLogin, request: Request):
|
||||
"""Login user"""
|
||||
client_ip = get_client_ip(request)
|
||||
print(f"Login request from {client_ip}: {user_login.model_dump(exclude={'password'})}")
|
||||
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
try:
|
||||
login_data = user_login.model_dump()
|
||||
print(f"Sending login data to user service: {login_data}")
|
||||
|
||||
response = await client.post(
|
||||
f"{SERVICES['users']}/api/v1/auth/login",
|
||||
json=user_login.model_dump(),
|
||||
json=login_data,
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
}
|
||||
)
|
||||
|
||||
print(f"User service response: status={response.status_code}")
|
||||
if response.status_code >= 400:
|
||||
print(f"Error response body: {response.text}")
|
||||
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
elif response.status_code == 422:
|
||||
# Detailed handling for validation errors
|
||||
try:
|
||||
error_json = response.json()
|
||||
print(f"Validation error details: {error_json}")
|
||||
# Return more detailed validation errors
|
||||
if "detail" in error_json:
|
||||
detail = error_json["detail"]
|
||||
if isinstance(detail, list):
|
||||
# FastAPI validation errors
|
||||
formatted_errors = []
|
||||
for error in detail:
|
||||
field = error.get("loc", ["unknown"])[-1]
|
||||
msg = error.get("msg", "Invalid value")
|
||||
formatted_errors.append(f"{field}: {msg}")
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail=f"Validation errors: {'; '.join(formatted_errors)}"
|
||||
)
|
||||
else:
|
||||
raise HTTPException(status_code=422, detail=detail)
|
||||
else:
|
||||
raise HTTPException(status_code=422, detail="Invalid input data")
|
||||
except ValueError as ve:
|
||||
print(f"JSON parse error: {ve}")
|
||||
raise HTTPException(status_code=422, detail="Invalid request format")
|
||||
else:
|
||||
error_detail = response.text
|
||||
try:
|
||||
@@ -272,6 +308,7 @@ async def login_user(user_login: UserLogin, request: Request):
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
print(f"Login service error: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"Login error: {str(e)}")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user