Update components/auth.py
Browse files- components/auth.py +35 -19
components/auth.py
CHANGED
|
@@ -4,38 +4,40 @@ from utils.db import sign_up_user, login_user
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
def register(email, password):
|
| 7 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 8 |
if not email or not password:
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
result = sign_up_user(email, password)
|
| 12 |
|
| 13 |
-
if result
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
if result.user:
|
| 17 |
if result.user and not result.session:
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
if hasattr(result, 'error') and result.error:
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
def login(email, password, current_user_state):
|
| 28 |
-
"""
|
| 29 |
-
Handler for the login process.
|
| 30 |
-
On success, returns a command to switch the UI to the 'prediction_tab'.
|
| 31 |
-
"""
|
| 32 |
if not email or not password:
|
| 33 |
return "❌ Email and password cannot be empty.", current_user_state, gr.update()
|
| 34 |
|
| 35 |
result = login_user(email, password)
|
| 36 |
|
| 37 |
if result and result.session:
|
| 38 |
-
new_user_state = {"email": email, "logged_in": True}
|
| 39 |
return f"✅ Login successful! Redirecting...", new_user_state, gr.Tabs(selected="prediction_tab")
|
| 40 |
|
| 41 |
if hasattr(result, 'error') and result.error:
|
|
@@ -47,8 +49,22 @@ def login(email, password, current_user_state):
|
|
| 47 |
def logout(current_user_state):
|
| 48 |
"""
|
| 49 |
Handler for the logout process.
|
| 50 |
-
|
| 51 |
"""
|
| 52 |
-
new_user_state = {"email": None, "logged_in": False}
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
def register(email, password):
|
| 7 |
+
"""
|
| 8 |
+
Handler for the signup process.
|
| 9 |
+
On success, it will now also return empty strings to clear the input fields.
|
| 10 |
+
"""
|
| 11 |
if not email or not password:
|
| 12 |
+
# Return a no-op update for the textboxes on failure
|
| 13 |
+
return "❌ Email and password cannot be empty.", gr.update(), gr.update()
|
| 14 |
|
| 15 |
result = sign_up_user(email, password)
|
| 16 |
|
| 17 |
+
if result and result.user:
|
| 18 |
+
message = "✅ Signup successful! You can now log in."
|
|
|
|
|
|
|
| 19 |
if result.user and not result.session:
|
| 20 |
+
message = "✅ Signup successful! Please check your email to confirm your account."
|
| 21 |
+
# On success, clear the signup email and password fields by returning empty strings
|
| 22 |
+
return message, "", ""
|
| 23 |
|
| 24 |
if hasattr(result, 'error') and result.error:
|
| 25 |
+
# Return a no-op update for the textboxes on failure
|
| 26 |
+
return f"❌ Signup Failed: {result.error.message}", gr.update(), gr.update()
|
| 27 |
|
| 28 |
+
# Return a no-op update for the textboxes on failure
|
| 29 |
+
return "❌ Signup failed. An unknown error occurred.", gr.update(), gr.update()
|
| 30 |
|
| 31 |
|
| 32 |
def login(email, password, current_user_state):
|
| 33 |
+
"""Handler for the login process."""
|
|
|
|
|
|
|
|
|
|
| 34 |
if not email or not password:
|
| 35 |
return "❌ Email and password cannot be empty.", current_user_state, gr.update()
|
| 36 |
|
| 37 |
result = login_user(email, password)
|
| 38 |
|
| 39 |
if result and result.session:
|
| 40 |
+
new_user_state = {"email": email, "id": result.user.id, "logged_in": True}
|
| 41 |
return f"✅ Login successful! Redirecting...", new_user_state, gr.Tabs(selected="prediction_tab")
|
| 42 |
|
| 43 |
if hasattr(result, 'error') and result.error:
|
|
|
|
| 49 |
def logout(current_user_state):
|
| 50 |
"""
|
| 51 |
Handler for the logout process.
|
| 52 |
+
Now also returns values to clear the login and prediction forms.
|
| 53 |
"""
|
| 54 |
+
new_user_state = {"email": None, "id": None, "logged_in": False}
|
| 55 |
+
|
| 56 |
+
# Message, new state, redirect, then empty values for all fields to be cleared
|
| 57 |
+
# The order of these return values MUST match the 'outputs' list in app.py
|
| 58 |
+
return (
|
| 59 |
+
"👋 Logged out successfully.",
|
| 60 |
+
new_user_state,
|
| 61 |
+
gr.Tabs(selected="home_tab"),
|
| 62 |
+
"", # Clear login email textbox
|
| 63 |
+
"", # Clear login password textbox
|
| 64 |
+
None, # Clear pregnancies number input
|
| 65 |
+
None, # Clear glucose number input
|
| 66 |
+
None, # Clear blood pressure number input
|
| 67 |
+
None, # Clear insulin number input
|
| 68 |
+
None, # Clear BMI number input
|
| 69 |
+
None # Clear age number input
|
| 70 |
+
)
|