Spaces:
Running
Running
Update auth.py
Browse files
auth.py
CHANGED
|
@@ -2,14 +2,24 @@ import json
|
|
| 2 |
from huggingface_hub import HfApi, hf_hub_download
|
| 3 |
from werkzeug.security import generate_password_hash, check_password_hash
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
-
def register_user(username, password, logged_in_username, auth_globals):
|
| 7 |
hf_token = auth_globals["hf_token"]
|
| 8 |
user_repo = auth_globals["user_repo"]
|
| 9 |
hf_api = HfApi(token=hf_token)
|
| 10 |
|
| 11 |
-
if
|
| 12 |
-
return [], None, [], gr.update(choices=[], value=None), logged_in_username, "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
try:
|
| 15 |
try:
|
|
@@ -18,90 +28,36 @@ def register_user(username, password, logged_in_username, auth_globals):
|
|
| 18 |
except:
|
| 19 |
pass
|
| 20 |
|
| 21 |
-
user_data = {"password": generate_password_hash(password), "chats": []}
|
| 22 |
-
hf_api.upload_file(
|
| 23 |
-
path_or_fileobj=json.dumps(user_data).encode('utf-8'),
|
| 24 |
-
path_in_repo=f"{username}.json",
|
| 25 |
-
repo_id=user_repo,
|
| 26 |
-
repo_type="dataset",
|
| 27 |
-
token=hf_token
|
| 28 |
-
)
|
| 29 |
-
chats = []
|
| 30 |
-
current_title = None
|
| 31 |
-
history = []
|
| 32 |
-
dropdown = gr.update(choices=[], value=None)
|
| 33 |
-
return chats, current_title, history, dropdown, username, "Registration successful! Logged in. Click 'β New Chat' to begin.", gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
|
| 34 |
-
except Exception as e:
|
| 35 |
-
return [], None, [], gr.update(choices=[], value=None), logged_in_username, f"Registration failed: {str(e)}", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
|
| 36 |
-
|
| 37 |
-
def login_user(username, password, logged_in_username, auth_globals):
|
| 38 |
-
hf_token = auth_globals["hf_token"]
|
| 39 |
-
user_repo = auth_globals["user_repo"]
|
| 40 |
-
hf_api = HfApi(token=hf_token)
|
| 41 |
-
|
| 42 |
-
if not username or not password:
|
| 43 |
-
return [], None, [], gr.update(choices=[], value=None), logged_in_username, "Please enter username and password.", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
|
| 44 |
-
|
| 45 |
-
try:
|
| 46 |
-
user_file = hf_hub_download(repo_id=user_repo, filename=f"{username}.json", repo_type="dataset", token=hf_token)
|
| 47 |
-
with open(user_file, 'r') as f:
|
| 48 |
-
data = json.load(f)
|
| 49 |
-
|
| 50 |
-
if check_password_hash(data["password"], password):
|
| 51 |
-
if "chats" in data and data["chats"]:
|
| 52 |
-
chats = data["chats"]
|
| 53 |
-
elif "history" in data and data["history"]:
|
| 54 |
-
chats = [{"title": "Imported Chat", "history": data["history"]}]
|
| 55 |
-
else:
|
| 56 |
-
chats = []
|
| 57 |
|
| 58 |
-
|
| 59 |
-
current_title = chats[-1]["title"]
|
| 60 |
-
history = chats[-1]["history"]
|
| 61 |
-
choices = [chat["title"] for chat in chats]
|
| 62 |
-
value = current_title
|
| 63 |
-
else:
|
| 64 |
-
current_title = None
|
| 65 |
-
history = []
|
| 66 |
-
choices = []
|
| 67 |
-
value = None
|
| 68 |
-
|
| 69 |
-
dropdown = gr.update(choices=choices, value=value)
|
| 70 |
-
return chats, current_title, history, dropdown, username, "Login successful! β
", gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
|
| 71 |
-
else:
|
| 72 |
-
return [], None, [], gr.update(choices=[], value=None), logged_in_username, "Incorrect password.", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
|
| 73 |
except Exception as e:
|
| 74 |
-
return [], None, [], gr.update(choices=[], value=None), logged_in_username, f"
|
| 75 |
-
|
| 76 |
-
def logout_user():
|
| 77 |
-
return [], None, [], gr.update(choices=[], value=None), None, "Logged out successfully!", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
if not username: return "No user logged in!"
|
| 81 |
-
hf_token = auth_globals["hf_token"]
|
| 82 |
-
user_repo = auth_globals["user_repo"]
|
| 83 |
-
hf_api = HfApi(token=hf_token)
|
| 84 |
-
try:
|
| 85 |
-
user_file = hf_hub_download(repo_id=user_repo, filename=f"{username}.json", repo_type="dataset", token=hf_token)
|
| 86 |
-
with open(user_file, 'r') as f:
|
| 87 |
-
data = json.load(f)
|
| 88 |
-
data["chats"] = chats
|
| 89 |
-
hf_api.upload_file(path_or_fileobj=json.dumps(data).encode('utf-8'), path_in_repo=f"{username}.json", repo_id=user_repo, repo_type="dataset", token=hf_token)
|
| 90 |
-
return "Chats saved automatically!"
|
| 91 |
-
except Exception as e:
|
| 92 |
-
return f"Failed to save chats: {str(e)}"
|
| 93 |
|
| 94 |
def delete_account(confirm_username, logged_in_username, auth_globals):
|
| 95 |
-
if not logged_in_username:
|
| 96 |
-
return "
|
| 97 |
-
if confirm_username != logged_in_username:
|
| 98 |
-
return "β Username does not match. Deletion cancelled.", logged_in_username, gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), None, None, None, None
|
| 99 |
|
| 100 |
hf_token = auth_globals["hf_token"]
|
| 101 |
user_repo = auth_globals["user_repo"]
|
| 102 |
hf_api = HfApi(token=hf_token)
|
| 103 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
hf_api.delete_file(path_in_repo=f"{logged_in_username}.json", repo_id=user_repo, repo_type="dataset", token=hf_token)
|
| 105 |
-
return "β
Account and all data permanently deleted.
|
| 106 |
except Exception as e:
|
| 107 |
return f"β Deletion failed: {str(e)}", logged_in_username, gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), None, None, None, None
|
|
|
|
| 2 |
from huggingface_hub import HfApi, hf_hub_download
|
| 3 |
from werkzeug.security import generate_password_hash, check_password_hash
|
| 4 |
import gradio as gr
|
| 5 |
+
from phone_verification import is_phone_available, send_dummy_otp, verify_otp, record_phone_in_user, record_cooldown, get_total_accounts
|
| 6 |
|
| 7 |
+
def register_user(username, password, phone, otp, logged_in_username, auth_globals):
|
| 8 |
hf_token = auth_globals["hf_token"]
|
| 9 |
user_repo = auth_globals["user_repo"]
|
| 10 |
hf_api = HfApi(token=hf_token)
|
| 11 |
|
| 12 |
+
if get_total_accounts(auth_globals) >= 100:
|
| 13 |
+
return [], None, [], gr.update(choices=[], value=None), logged_in_username, "β Maximum 100 accounts reached.", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
|
| 14 |
+
|
| 15 |
+
if not username or not password or not phone:
|
| 16 |
+
return [], None, [], gr.update(choices=[], value=None), logged_in_username, "Please fill all fields.", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
|
| 17 |
+
|
| 18 |
+
if not is_phone_available(phone, auth_globals):
|
| 19 |
+
return [], None, [], gr.update(choices=[], value=None), logged_in_username, "β This phone number is already used or in cooldown.", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
|
| 20 |
+
|
| 21 |
+
if not verify_otp(phone, otp, auth_globals):
|
| 22 |
+
return [], None, [], gr.update(choices=[], value=None), logged_in_username, "β Wrong OTP.", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
|
| 23 |
|
| 24 |
try:
|
| 25 |
try:
|
|
|
|
| 28 |
except:
|
| 29 |
pass
|
| 30 |
|
| 31 |
+
user_data = {"password": generate_password_hash(password), "chats": [], "phone": phone}
|
| 32 |
+
hf_api.upload_file(path_or_fileobj=json.dumps(user_data).encode('utf-8'), path_in_repo=f"{username}.json", repo_id=user_repo, repo_type="dataset", token=hf_token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
return [], None, [], gr.update(choices=[], value=None), username, "β
Registration successful! Logged in.", gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
+
return [], None, [], gr.update(choices=[], value=None), logged_in_username, f"Registration failed: {str(e)}", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# ... (login_user, logout_user, save_chats, delete_account remain the same, but delete_account now calls record_cooldown if phone exists)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def delete_account(confirm_username, logged_in_username, auth_globals):
|
| 41 |
+
if not logged_in_username or confirm_username != logged_in_username:
|
| 42 |
+
return "β Username does not match.", logged_in_username, gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), None, None, None, None
|
|
|
|
|
|
|
| 43 |
|
| 44 |
hf_token = auth_globals["hf_token"]
|
| 45 |
user_repo = auth_globals["user_repo"]
|
| 46 |
hf_api = HfApi(token=hf_token)
|
| 47 |
try:
|
| 48 |
+
# Get phone before delete
|
| 49 |
+
try:
|
| 50 |
+
file = hf_hub_download(repo_id=user_repo, filename=f"{logged_in_username}.json", repo_type="dataset", token=hf_token)
|
| 51 |
+
with open(file, 'r') as f:
|
| 52 |
+
data = json.load(f)
|
| 53 |
+
phone = data.get("phone")
|
| 54 |
+
if phone:
|
| 55 |
+
from phone_verification import record_cooldown
|
| 56 |
+
record_cooldown(phone, auth_globals)
|
| 57 |
+
except:
|
| 58 |
+
pass
|
| 59 |
+
|
| 60 |
hf_api.delete_file(path_in_repo=f"{logged_in_username}.json", repo_id=user_repo, repo_type="dataset", token=hf_token)
|
| 61 |
+
return "β
Account and all data permanently deleted.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), [], None, [], gr.update(choices=[], value=None)
|
| 62 |
except Exception as e:
|
| 63 |
return f"β Deletion failed: {str(e)}", logged_in_username, gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), None, None, None, None
|