qwendashcoder commited on
Commit
d8036bc
Β·
verified Β·
1 Parent(s): bf7cd1f

Update auth.py

Browse files
Files changed (1) hide show
  1. auth.py +78 -10
auth.py CHANGED
@@ -2,41 +2,109 @@ 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
- 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:
26
  hf_hub_download(repo_id=user_repo, filename=f"{username}.json", repo_type="dataset", token=hf_token)
27
  return [], None, [], gr.update(choices=[], value=None), logged_in_username, "User already exists!", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
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
@@ -45,7 +113,7 @@ def delete_account(confirm_username, logged_in_username, auth_globals):
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:
@@ -58,6 +126,6 @@ def delete_account(confirm_username, logged_in_username, auth_globals):
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
 
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, verify_otp, record_phone_in_user, record_cooldown, get_total_accounts
6
 
7
+ # ================== REGISTER ==================
8
  def register_user(username, password, phone, otp, logged_in_username, auth_globals):
9
  hf_token = auth_globals["hf_token"]
10
  user_repo = auth_globals["user_repo"]
11
  hf_api = HfApi(token=hf_token)
12
 
13
  if get_total_accounts(auth_globals) >= 100:
14
+ return [], None, [], gr.update(choices=[], value=None), logged_in_username, "❌ Maximum 100 accounts reached for this project.", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
15
 
16
+ if not username or not password or not phone or not otp:
17
  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)
18
 
19
  if not is_phone_available(phone, auth_globals):
20
+ return [], None, [], gr.update(choices=[], value=None), logged_in_username, "❌ This phone number is already used or in 3-hour cooldown.", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
21
 
22
  if not verify_otp(phone, otp, auth_globals):
23
+ return [], None, [], gr.update(choices=[], value=None), logged_in_username, "❌ Wrong OTP. Use 123456 for testing.", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
24
 
25
  try:
26
+ # Check if username exists
27
  try:
28
  hf_hub_download(repo_id=user_repo, filename=f"{username}.json", repo_type="dataset", token=hf_token)
29
  return [], None, [], gr.update(choices=[], value=None), logged_in_username, "User already exists!", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
30
  except:
31
  pass
32
 
33
+ user_data = {
34
+ "password": generate_password_hash(password),
35
+ "chats": [],
36
+ "phone": phone
37
+ }
38
+ hf_api.upload_file(
39
+ path_or_fileobj=json.dumps(user_data).encode('utf-8'),
40
+ path_in_repo=f"{username}.json",
41
+ repo_id=user_repo,
42
+ repo_type="dataset",
43
+ token=hf_token
44
+ )
45
+
46
+ record_phone_in_user(username, phone, auth_globals)
47
 
48
  return [], None, [], gr.update(choices=[], value=None), username, "βœ… Registration successful! Logged in.", gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
49
  except Exception as e:
50
  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)
51
 
52
+ # ================== LOGIN ==================
53
+ def login_user(username, password, logged_in_username, auth_globals):
54
+ hf_token = auth_globals["hf_token"]
55
+ user_repo = auth_globals["user_repo"]
56
+ hf_api = HfApi(token=hf_token)
57
+
58
+ if not username or not password:
59
+ 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)
60
+
61
+ try:
62
+ user_file = hf_hub_download(repo_id=user_repo, filename=f"{username}.json", repo_type="dataset", token=hf_token)
63
+ with open(user_file, 'r') as f:
64
+ data = json.load(f)
65
+
66
+ if check_password_hash(data["password"], password):
67
+ chats = data.get("chats", [])
68
+ if chats:
69
+ current_title = chats[-1]["title"]
70
+ history = chats[-1]["history"]
71
+ choices = [chat["title"] for chat in chats]
72
+ value = current_title
73
+ else:
74
+ current_title = None
75
+ history = []
76
+ choices = []
77
+ value = None
78
+
79
+ dropdown = gr.update(choices=choices, value=value)
80
+ return chats, current_title, history, dropdown, username, "βœ… Login successful!", gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
81
+ else:
82
+ 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)
83
+ except Exception as e:
84
+ return [], None, [], gr.update(choices=[], value=None), logged_in_username, f"Login failed: {str(e)}", gr.update(visible=bool(logged_in_username)), gr.update(visible=False), gr.update(visible=False)
85
+
86
+ # ================== LOGOUT ==================
87
+ def logout_user():
88
+ return [], None, [], gr.update(choices=[], value=None), None, "Logged out successfully!", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
89
+
90
+ # ================== SAVE CHATS ==================
91
+ def save_chats(username, chats, auth_globals):
92
+ if not username:
93
+ return "No user logged in!"
94
+ hf_token = auth_globals["hf_token"]
95
+ user_repo = auth_globals["user_repo"]
96
+ hf_api = HfApi(token=hf_token)
97
+ try:
98
+ user_file = hf_hub_download(repo_id=user_repo, filename=f"{username}.json", repo_type="dataset", token=hf_token)
99
+ with open(user_file, 'r') as f:
100
+ data = json.load(f)
101
+ data["chats"] = chats
102
+ 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)
103
+ return "Chats saved automatically!"
104
+ except Exception as e:
105
+ return f"Failed to save chats: {str(e)}"
106
 
107
+ # ================== DELETE ACCOUNT ==================
108
  def delete_account(confirm_username, logged_in_username, auth_globals):
109
  if not logged_in_username or confirm_username != logged_in_username:
110
  return "❌ Username does not match.", logged_in_username, gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), None, None, None, None
 
113
  user_repo = auth_globals["user_repo"]
114
  hf_api = HfApi(token=hf_token)
115
  try:
116
+ # Get phone before deleting
117
  try:
118
  file = hf_hub_download(repo_id=user_repo, filename=f"{logged_in_username}.json", repo_type="dataset", token=hf_token)
119
  with open(file, 'r') as f:
 
126
  pass
127
 
128
  hf_api.delete_file(path_in_repo=f"{logged_in_username}.json", repo_id=user_repo, repo_type="dataset", token=hf_token)
129
+ return "βœ… Account permanently deleted.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), [], None, [], gr.update(choices=[], value=None)
130
  except Exception as e:
131
  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