energensy contacts, dashboard
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -9,6 +9,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.openapi.utils import get_openapi
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Any
|
||||
|
||||
from shared.config import settings
|
||||
from services.user_service.schemas import UserCreate, UserLogin, UserResponse, UserUpdate, Token
|
||||
@@ -248,7 +249,17 @@ async def register_user(user_create: UserCreate, request: Request):
|
||||
async def login_user(user_login: UserLogin, request: Request):
|
||||
"""Login user"""
|
||||
client_ip = get_client_ip(request)
|
||||
|
||||
# Дополнительное логирование для отладки
|
||||
try:
|
||||
request_body = await request.body()
|
||||
print(f"RAW request body from {client_ip}: {request_body}")
|
||||
print(f"Request headers: {dict(request.headers)}")
|
||||
except:
|
||||
pass
|
||||
|
||||
print(f"Login request from {client_ip}: {user_login.model_dump(exclude={'password'})}")
|
||||
print(f"Full login data: {user_login.model_dump()}")
|
||||
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
try:
|
||||
@@ -312,6 +323,113 @@ async def login_user(user_login: UserLogin, request: Request):
|
||||
raise HTTPException(status_code=500, detail=f"Login error: {str(e)}")
|
||||
|
||||
|
||||
# Debug endpoint to analyze raw request data
|
||||
@app.post("/api/v1/debug/login", tags=["Debug"], summary="Debug login request data")
|
||||
async def debug_login_request(request: Request):
|
||||
"""Debug endpoint to analyze raw request data from mobile app"""
|
||||
try:
|
||||
client_ip = get_client_ip(request)
|
||||
headers = dict(request.headers)
|
||||
body = await request.body()
|
||||
|
||||
debug_info = {
|
||||
"client_ip": client_ip,
|
||||
"headers": headers,
|
||||
"raw_body": body.decode('utf-8', errors='ignore') if body else None,
|
||||
"content_length": len(body) if body else 0,
|
||||
"content_type": headers.get('content-type', 'unknown')
|
||||
}
|
||||
|
||||
print(f"DEBUG LOGIN from {client_ip}:")
|
||||
print(f"Headers: {headers}")
|
||||
print(f"Raw body: {body}")
|
||||
|
||||
# Try to parse as JSON
|
||||
try:
|
||||
if body:
|
||||
import json
|
||||
json_data = json.loads(body)
|
||||
debug_info["parsed_json"] = json_data
|
||||
print(f"Parsed JSON: {json_data}")
|
||||
except Exception as e:
|
||||
debug_info["json_parse_error"] = str(e)
|
||||
print(f"JSON parse error: {e}")
|
||||
|
||||
return debug_info
|
||||
|
||||
except Exception as e:
|
||||
print(f"Debug endpoint error: {str(e)}")
|
||||
return {"error": str(e)}
|
||||
|
||||
|
||||
# Alternative login endpoint for mobile app debugging
|
||||
@app.post("/api/v1/auth/login-flexible", tags=["Authentication"], summary="Flexible login for debugging")
|
||||
async def login_flexible(request: Request):
|
||||
"""Flexible login endpoint that accepts various data formats"""
|
||||
try:
|
||||
client_ip = get_client_ip(request)
|
||||
body = await request.body()
|
||||
|
||||
print(f"Flexible login from {client_ip}")
|
||||
print(f"Raw request: {body}")
|
||||
|
||||
# Try to parse JSON
|
||||
import json
|
||||
try:
|
||||
data = json.loads(body) if body else {}
|
||||
except:
|
||||
return {"error": "Invalid JSON format", "status": 422}
|
||||
|
||||
print(f"Parsed data: {data}")
|
||||
|
||||
# Extract login fields with flexible names
|
||||
email = data.get('email') or data.get('Email') or data.get('EMAIL')
|
||||
username = data.get('username') or data.get('Username') or data.get('user_name') or data.get('login')
|
||||
password = data.get('password') or data.get('Password') or data.get('PASSWORD') or data.get('pass')
|
||||
|
||||
print(f"Extracted: email={email}, username={username}, password={'***' if password else None}")
|
||||
|
||||
# Validation
|
||||
if not password:
|
||||
return {"error": "Password is required", "status": 422}
|
||||
|
||||
if not email and not username:
|
||||
return {"error": "Either email or username must be provided", "status": 422}
|
||||
|
||||
# Create proper login data
|
||||
login_data = {
|
||||
"email": email,
|
||||
"username": username,
|
||||
"password": password
|
||||
}
|
||||
|
||||
# Forward to user service
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
response = await client.post(
|
||||
f"{SERVICES['users']}/api/v1/auth/login",
|
||||
json=login_data,
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
}
|
||||
)
|
||||
|
||||
print(f"User service response: {response.status_code} - {response.text}")
|
||||
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
try:
|
||||
error_data = response.json()
|
||||
return {"error": error_data.get("detail", "Login failed"), "status": response.status_code}
|
||||
except:
|
||||
return {"error": response.text, "status": response.status_code}
|
||||
|
||||
except Exception as e:
|
||||
print(f"Flexible login error: {str(e)}")
|
||||
return {"error": str(e), "status": 500}
|
||||
|
||||
|
||||
# Utility endpoints
|
||||
@app.get("/api/v1/auth/check-email", tags=["Authentication"], summary="Check if email is available")
|
||||
async def check_email_availability(email: str):
|
||||
|
||||
@@ -372,6 +372,58 @@ async def delete_emergency_contact(
|
||||
return None
|
||||
|
||||
|
||||
@app.get("/api/v1/users/dashboard", tags=["Dashboard"])
|
||||
async def get_user_dashboard(
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""Get user dashboard data"""
|
||||
try:
|
||||
# Get emergency contacts count
|
||||
emergency_contacts_result = await db.execute(
|
||||
select(EmergencyContact).filter(
|
||||
EmergencyContact.user_id == current_user.id,
|
||||
EmergencyContact.is_active == True
|
||||
)
|
||||
)
|
||||
emergency_contacts = emergency_contacts_result.scalars().all()
|
||||
|
||||
dashboard_data = {
|
||||
"user": {
|
||||
"id": current_user.id,
|
||||
"uuid": str(current_user.uuid),
|
||||
"email": current_user.email,
|
||||
"username": current_user.username,
|
||||
"first_name": current_user.first_name,
|
||||
"last_name": current_user.last_name,
|
||||
"avatar_url": current_user.avatar_url
|
||||
},
|
||||
"emergency_contacts_count": len(emergency_contacts),
|
||||
"settings": {
|
||||
"location_sharing_enabled": current_user.location_sharing_enabled,
|
||||
"emergency_notifications_enabled": current_user.emergency_notifications_enabled,
|
||||
"push_notifications_enabled": current_user.push_notifications_enabled
|
||||
},
|
||||
"verification_status": {
|
||||
"email_verified": current_user.email_verified,
|
||||
"phone_verified": current_user.phone_verified
|
||||
},
|
||||
"account_status": {
|
||||
"is_active": current_user.is_active,
|
||||
"created_at": str(current_user.created_at) if hasattr(current_user, 'created_at') else None
|
||||
}
|
||||
}
|
||||
|
||||
return dashboard_data
|
||||
|
||||
except Exception as e:
|
||||
print(f"Dashboard error: {str(e)}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to load dashboard data"
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/v1/health")
|
||||
async def health_check_v1():
|
||||
"""Health check endpoint with API version"""
|
||||
|
||||
Reference in New Issue
Block a user