Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -72,7 +72,7 @@ class TokenManager:
|
|
| 72 |
json=payload.dict(),
|
| 73 |
timeout=10
|
| 74 |
) as response:
|
| 75 |
-
logger.debug(f"Login response status: {response.status}")
|
| 76 |
if response.status == 200:
|
| 77 |
data = await response.json()
|
| 78 |
token = data.get("access_token")
|
|
@@ -83,10 +83,10 @@ class TokenManager:
|
|
| 83 |
return token
|
| 84 |
else:
|
| 85 |
error = await response.text()
|
| 86 |
-
logger.error(f"Login failed: {response.status} - {error}")
|
| 87 |
return None
|
| 88 |
except aiohttp.ClientError as e:
|
| 89 |
-
logger.error(f"Login request error: {str(e)}")
|
| 90 |
return None
|
| 91 |
|
| 92 |
async def refresh_token(self) -> str:
|
|
@@ -125,15 +125,31 @@ async def redirect_login(request: Request):
|
|
| 125 |
logger.info("Redirecting /login to /admin-auth")
|
| 126 |
return RedirectResponse(url="/admin-auth", status_code=307)
|
| 127 |
|
| 128 |
-
# Handle local /auth/login requests
|
| 129 |
@app.post("/auth/login")
|
| 130 |
async def handle_local_auth_login(request: Request):
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
raise HTTPException(
|
| 133 |
status_code=400,
|
| 134 |
-
detail="
|
| 135 |
)
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
def authenticate_admin(email: str = None, password: str = None):
|
| 138 |
if email != ADMIN_EMAIL or password != ADMIN_PASSWORD:
|
| 139 |
logger.warning(f"Failed admin login attempt with email: {email}")
|
|
@@ -172,7 +188,7 @@ async def async_create_doctor(full_name: str, email: str, license_number: str, s
|
|
| 172 |
headers=headers,
|
| 173 |
timeout=10
|
| 174 |
) as response:
|
| 175 |
-
logger.debug(f"Doctor creation response status: {response.status}")
|
| 176 |
if response.status == 201:
|
| 177 |
return "✅ Doctor created successfully!"
|
| 178 |
elif response.status == 401:
|
|
@@ -185,7 +201,7 @@ async def async_create_doctor(full_name: str, email: str, license_number: str, s
|
|
| 185 |
headers=headers,
|
| 186 |
timeout=10
|
| 187 |
) as retry_response:
|
| 188 |
-
logger.debug(f"Retry doctor creation response status: {retry_response.status}")
|
| 189 |
if retry_response.status == 201:
|
| 190 |
return "✅ Doctor created successfully!"
|
| 191 |
error_detail = await retry_response.text()
|
|
|
|
| 72 |
json=payload.dict(),
|
| 73 |
timeout=10
|
| 74 |
) as response:
|
| 75 |
+
logger.debug(f"Login response status: {response.status}, URL: {response.url}")
|
| 76 |
if response.status == 200:
|
| 77 |
data = await response.json()
|
| 78 |
token = data.get("access_token")
|
|
|
|
| 83 |
return token
|
| 84 |
else:
|
| 85 |
error = await response.text()
|
| 86 |
+
logger.error(f"Login failed: {response.status} - {error}, URL: {response.url}")
|
| 87 |
return None
|
| 88 |
except aiohttp.ClientError as e:
|
| 89 |
+
logger.error(f"Login request error: {str(e)}, URL: {login_url}")
|
| 90 |
return None
|
| 91 |
|
| 92 |
async def refresh_token(self) -> str:
|
|
|
|
| 125 |
logger.info("Redirecting /login to /admin-auth")
|
| 126 |
return RedirectResponse(url="/admin-auth", status_code=307)
|
| 127 |
|
| 128 |
+
# Handle local /auth/login requests
|
| 129 |
@app.post("/auth/login")
|
| 130 |
async def handle_local_auth_login(request: Request):
|
| 131 |
+
try:
|
| 132 |
+
body = await request.json()
|
| 133 |
+
except:
|
| 134 |
+
body = {}
|
| 135 |
+
logger.warning(f"Local /auth/login endpoint called with body: {body}")
|
| 136 |
raise HTTPException(
|
| 137 |
status_code=400,
|
| 138 |
+
detail=f"Local /auth/login called incorrectly. Use the external backend API: {BACKEND_URL}/auth/login"
|
| 139 |
)
|
| 140 |
|
| 141 |
+
# Test endpoint to verify backend connectivity
|
| 142 |
+
@app.get("/test-backend")
|
| 143 |
+
async def test_backend():
|
| 144 |
+
try:
|
| 145 |
+
async with aiohttp.ClientSession() as session:
|
| 146 |
+
async with session.get(BACKEND_URL, timeout=5) as response:
|
| 147 |
+
logger.debug(f"Test backend response status: {response.status}, URL: {response.url}")
|
| 148 |
+
return {"status": response.status, "url": str(response.url), "message": await response.text()}
|
| 149 |
+
except aiohttp.ClientError as e:
|
| 150 |
+
logger.error(f"Test backend error: {str(e)}")
|
| 151 |
+
return {"status": "error", "message": str(e)}
|
| 152 |
+
|
| 153 |
def authenticate_admin(email: str = None, password: str = None):
|
| 154 |
if email != ADMIN_EMAIL or password != ADMIN_PASSWORD:
|
| 155 |
logger.warning(f"Failed admin login attempt with email: {email}")
|
|
|
|
| 188 |
headers=headers,
|
| 189 |
timeout=10
|
| 190 |
) as response:
|
| 191 |
+
logger.debug(f"Doctor creation response status: {response.status}, URL: {response.url}")
|
| 192 |
if response.status == 201:
|
| 193 |
return "✅ Doctor created successfully!"
|
| 194 |
elif response.status == 401:
|
|
|
|
| 201 |
headers=headers,
|
| 202 |
timeout=10
|
| 203 |
) as retry_response:
|
| 204 |
+
logger.debug(f"Retry doctor creation response status: {retry_response.status}, URL: {retry_response.url}")
|
| 205 |
if retry_response.status == 201:
|
| 206 |
return "✅ Doctor created successfully!"
|
| 207 |
error_detail = await retry_response.text()
|