Update components/auth.py
Browse files- components/auth.py +35 -14
components/auth.py
CHANGED
|
@@ -1,20 +1,41 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
def register(email, password):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
result = sign_up_user(email, password)
|
| 7 |
-
if result.user:
|
| 8 |
-
return "β
Signup successful!"
|
| 9 |
-
return "β Signup failed."
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
result = login_user(email, password)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# components/auth.py
|
| 2 |
|
| 3 |
+
from utils.db import sign_up_user, login_user
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
def register(email, password):
|
| 7 |
+
"""Handler for the signup process."""
|
| 8 |
+
if not email or not password:
|
| 9 |
+
return "β Email and password cannot be empty."
|
| 10 |
+
|
| 11 |
result = sign_up_user(email, password)
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
if result and result.user:
|
| 14 |
+
# Check for the common case where the user is created but needs to confirm email
|
| 15 |
+
if result.user and not result.session:
|
| 16 |
+
return "β
Signup successful! Please check your email to confirm your account."
|
| 17 |
+
return "β
Signup successful! You can now log in."
|
| 18 |
+
|
| 19 |
+
return "β Signup failed. The user may already exist or the password is too weak."
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def login(email, password, current_user_state):
|
| 23 |
+
"""Handler for the login process."""
|
| 24 |
+
if not email or not password:
|
| 25 |
+
return "β Email and password cannot be empty.", current_user_state
|
| 26 |
+
|
| 27 |
result = login_user(email, password)
|
| 28 |
+
|
| 29 |
+
if result and result.session:
|
| 30 |
+
# Update the user state
|
| 31 |
+
new_user_state = {"email": email, "logged_in": True}
|
| 32 |
+
return f"β
Logged in as {email}", new_user_state
|
| 33 |
+
|
| 34 |
+
return "β Login failed. Check your credentials or confirm your email.", current_user_state
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def logout(current_user_state):
|
| 38 |
+
"""Handler for the logout process."""
|
| 39 |
+
# Reset the user state
|
| 40 |
+
new_user_state = {"email": None, "logged_in": False}
|
| 41 |
+
return "π Logged out successfully.", new_user_state
|