change login
This commit is contained in:
@@ -26,35 +26,58 @@ class Login:
|
||||
self.error_message.update()
|
||||
return
|
||||
|
||||
# self.auth.placeholder.content.clean()
|
||||
# two_factor = TwoFactorAuth(self.page, email, self, self.auth)
|
||||
# self.auth.placeholder.content = two_factor.build()
|
||||
# self.auth.placeholder.update()
|
||||
|
||||
try:
|
||||
# Call login endpoint
|
||||
response = requests.post(
|
||||
f"{API_BASE_URL}/auth/login",
|
||||
json={"email": email, "password": password},
|
||||
timeout=10
|
||||
timeout=15,
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
user_info = requests.get(
|
||||
f"{API_BASE_URL}/auth/me",
|
||||
headers={"Authorization": f"Bearer {response.json().get('access_token', '')}"}
|
||||
)
|
||||
if user_info.status_code == 200:
|
||||
logo_filename = user_info.json().get("logo_filename", "")
|
||||
if logo_filename:
|
||||
self.page.client_storage.set("custom_logo", logo_filename)
|
||||
# Attempt to parse JSON safely
|
||||
resp_ct = response.headers.get("content-type", "")
|
||||
resp_json = None
|
||||
if "application/json" in resp_ct.lower():
|
||||
try:
|
||||
resp_json = response.json()
|
||||
except Exception:
|
||||
resp_json = None
|
||||
|
||||
if response.status_code == 200 and resp_json is not None:
|
||||
access_token = resp_json.get("access_token", "")
|
||||
# Fetch user info (best-effort)
|
||||
ui = requests.get(
|
||||
f"{API_BASE_URL}/auth/me",
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
timeout=10,
|
||||
)
|
||||
|
||||
if ui.status_code == 200:
|
||||
try:
|
||||
uj = ui.json()
|
||||
logo_filename = uj.get("logo_filename", "")
|
||||
if logo_filename:
|
||||
self.page.client_storage.set("custom_logo", logo_filename)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Proceed to 2FA
|
||||
self.auth.placeholder.content.clean()
|
||||
two_factor = TwoFactorAuth(self.page, email, self, self.auth)
|
||||
self.auth.placeholder.content = two_factor.build()
|
||||
self.auth.placeholder.update()
|
||||
|
||||
else:
|
||||
self.error_message.value = response.json().get("error", "Login failed")
|
||||
# Build readable error message
|
||||
err_msg = None
|
||||
if resp_json and isinstance(resp_json, dict):
|
||||
err_msg = resp_json.get("error") or resp_json.get("message")
|
||||
if not err_msg:
|
||||
# fallback to raw text / status code
|
||||
t = response.text.strip()
|
||||
err_msg = t if t else f"HTTP {response.status_code} from server"
|
||||
|
||||
self.error_message.value = f"Login failed: {err_msg}"
|
||||
self.error_message.update()
|
||||
|
||||
except Exception as ex:
|
||||
|
||||
Reference in New Issue
Block a user