FauziIsyrinApridal commited on
Commit
59b48be
Β·
1 Parent(s): 950f1d6
Files changed (3) hide show
  1. .env.example +1 -1
  2. app/auth.py +2 -58
  3. {app β†’ pages}/reset_password.py +15 -73
.env.example CHANGED
@@ -5,7 +5,7 @@ SUPABASE_KEY=
5
  SUPABASE_STORAGE_BUCKET=
6
 
7
  # Email redirect URL for Supabase auth (registration & password reset)
8
- SUPABASE_EMAIL_REDIRECT=https://yozora721-pnp-chatbot-v1.hf.space
9
  NEXT_PUBLIC_SITE_URL=https://yozora721-pnp-chatbot-v1.hf.space
10
 
11
  HUGGINGFACEHUB_API_TOKEN=
 
5
  SUPABASE_STORAGE_BUCKET=
6
 
7
  # Email redirect URL for Supabase auth (registration & password reset)
8
+ SUPABASE_EMAIL_REDIRECT=https://yozora721-pnp-chatbot-v1.hf.space/reset_password
9
  NEXT_PUBLIC_SITE_URL=https://yozora721-pnp-chatbot-v1.hf.space
10
 
11
  HUGGINGFACEHUB_API_TOKEN=
app/auth.py CHANGED
@@ -31,62 +31,6 @@ def auth_view():
31
  unsafe_allow_html=True
32
  )
33
 
34
- # --- Password recovery handler (Supabase redirect) ---
35
- # 1) Move hash params to query params on first load, then reload once
36
- st.markdown(
37
- """
38
- <script>
39
- (function(){
40
- // Check if we have hash parameters and haven't migrated yet
41
- const hash = window.location.hash;
42
- if (hash && hash.length > 1 && !sessionStorage.getItem('hash_migrated')) {
43
- // Remove the # and parse as URLSearchParams
44
- const hashParams = new URLSearchParams(hash.substring(1));
45
- const queryParams = new URLSearchParams(window.location.search);
46
-
47
- // Copy all hash params to query params
48
- let hasParams = false;
49
- for (const [key, value] of hashParams.entries()) {
50
- queryParams.set(key, value);
51
- hasParams = true;
52
- }
53
-
54
- if (hasParams) {
55
- // Build new URL with query params
56
- const newUrl = window.location.pathname + '?' + queryParams.toString();
57
-
58
- // Mark as migrated to prevent infinite loops
59
- sessionStorage.setItem('hash_migrated', 'true');
60
-
61
- // Replace current URL and clear hash
62
- window.history.replaceState(null, '', newUrl);
63
- window.location.hash = '';
64
-
65
- // Reload to let Streamlit process the new query params
66
- window.location.reload();
67
- }
68
- }
69
- })();
70
- </script>
71
- """,
72
- unsafe_allow_html=True,
73
- )
74
-
75
- # 2) Read query params for recovery flow
76
- try:
77
- qp = st.query_params # Streamlit >= 1.30
78
- get_q = lambda k: qp.get(k, None)
79
- except Exception:
80
- qp = st.experimental_get_query_params()
81
- get_q = lambda k: (qp.get(k, [None])[0] if isinstance(qp.get(k, None), list) else qp.get(k, None))
82
-
83
- q_type = get_q("type")
84
- if q_type == "recovery":
85
- # Redirect to dedicated reset password page
86
- from app.reset_password import reset_password_view, handle_hash_to_query_migration
87
- handle_hash_to_query_migration()
88
- reset_password_view()
89
- return
90
 
91
  # Auth tabs inside wrapper
92
  tab_login, tab_register, tab_forgot = st.tabs(["Login", "Register", "Forgot Password"])
@@ -140,7 +84,7 @@ def auth_view():
140
  redirect_url = os.getenv(
141
  "SUPABASE_EMAIL_REDIRECT",
142
  os.getenv("NEXT_PUBLIC_SITE_URL", "https://yozora721-pnp-chatbot-v1.hf.space"),
143
- )
144
  supabase.auth.sign_up({
145
  "email": r_email,
146
  "password": r_password,
@@ -161,7 +105,7 @@ def auth_view():
161
  redirect_url = os.getenv(
162
  "SUPABASE_EMAIL_REDIRECT",
163
  os.getenv("NEXT_PUBLIC_SITE_URL", "https://yozora721-pnp-chatbot-v1.hf.space"),
164
- )
165
  supabase.auth.reset_password_for_email(f_email, {"redirect_to": redirect_url})
166
  st.success("Email reset password telah dikirim. Periksa kotak masuk Anda.")
167
  except Exception as e:
 
31
  unsafe_allow_html=True
32
  )
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  # Auth tabs inside wrapper
36
  tab_login, tab_register, tab_forgot = st.tabs(["Login", "Register", "Forgot Password"])
 
84
  redirect_url = os.getenv(
85
  "SUPABASE_EMAIL_REDIRECT",
86
  os.getenv("NEXT_PUBLIC_SITE_URL", "https://yozora721-pnp-chatbot-v1.hf.space"),
87
+ ) + "/reset_password"
88
  supabase.auth.sign_up({
89
  "email": r_email,
90
  "password": r_password,
 
105
  redirect_url = os.getenv(
106
  "SUPABASE_EMAIL_REDIRECT",
107
  os.getenv("NEXT_PUBLIC_SITE_URL", "https://yozora721-pnp-chatbot-v1.hf.space"),
108
+ ) + "/reset_password"
109
  supabase.auth.reset_password_for_email(f_email, {"redirect_to": redirect_url})
110
  st.success("Email reset password telah dikirim. Periksa kotak masuk Anda.")
111
  except Exception as e:
{app β†’ pages}/reset_password.py RENAMED
@@ -4,8 +4,13 @@ import streamlit as st
4
  from app.db import supabase
5
 
6
 
7
- def reset_password_view():
8
- """Dedicated reset password page for Supabase password recovery."""
 
 
 
 
 
9
 
10
  # Center the content
11
  left, center, right = st.columns([1, 2, 1])
@@ -48,15 +53,7 @@ def reset_password_view():
48
  st.info("Silakan kembali ke halaman login dan minta link reset password yang baru.")
49
 
50
  if st.button("← Kembali ke Login", type="primary"):
51
- # Clear query params and redirect to main page
52
- st.markdown(
53
- """
54
- <script>
55
- window.location.href = window.location.origin + window.location.pathname;
56
- </script>
57
- """,
58
- unsafe_allow_html=True
59
- )
60
  return
61
 
62
  # Reset password form
@@ -83,15 +80,7 @@ def reset_password_view():
83
  cancel_reset = st.form_submit_button("❌ Batal", use_container_width=True)
84
 
85
  if cancel_reset:
86
- # Redirect to main page
87
- st.markdown(
88
- """
89
- <script>
90
- window.location.href = window.location.origin + window.location.pathname;
91
- </script>
92
- """,
93
- unsafe_allow_html=True
94
- )
95
 
96
  if submit_reset:
97
  # Validation
@@ -120,20 +109,11 @@ def reset_password_view():
120
  st.success("βœ… Password berhasil diubah!")
121
  st.info("Silakan login dengan password baru Anda.")
122
 
123
- # Clear tokens and redirect after success
124
- st.markdown(
125
- """
126
- <script>
127
- setTimeout(function() {
128
- window.location.href = window.location.origin + window.location.pathname;
129
- }, 3000);
130
- </script>
131
- """,
132
- unsafe_allow_html=True
133
- )
134
 
135
- # Show countdown
136
- st.markdown("*Akan dialihkan ke halaman login dalam 3 detik...*")
137
  else:
138
  st.error("❌ Gagal mengubah password. Silakan coba lagi.")
139
 
@@ -142,43 +122,5 @@ def reset_password_view():
142
  st.info("Link mungkin sudah kedaluwarsa. Silakan minta link reset password yang baru.")
143
 
144
 
145
- def handle_hash_to_query_migration():
146
- """Handle migration of hash parameters to query parameters for password recovery."""
147
- st.markdown(
148
- """
149
- <script>
150
- (function(){
151
- // Check if we have hash parameters and haven't migrated yet
152
- const hash = window.location.hash;
153
- if (hash && hash.length > 1 && !sessionStorage.getItem('hash_migrated_reset')) {
154
- // Remove the # and parse as URLSearchParams
155
- const hashParams = new URLSearchParams(hash.substring(1));
156
- const queryParams = new URLSearchParams(window.location.search);
157
-
158
- // Copy all hash params to query params
159
- let hasParams = false;
160
- for (const [key, value] of hashParams.entries()) {
161
- queryParams.set(key, value);
162
- hasParams = true;
163
- }
164
-
165
- if (hasParams) {
166
- // Build new URL with query params
167
- const newUrl = window.location.pathname + '?' + queryParams.toString();
168
-
169
- // Mark as migrated to prevent infinite loops
170
- sessionStorage.setItem('hash_migrated_reset', 'true');
171
-
172
- // Replace current URL and clear hash
173
- window.history.replaceState(null, '', newUrl);
174
- window.location.hash = '';
175
-
176
- // Reload to let Streamlit process the new query params
177
- window.location.reload();
178
- }
179
- }
180
- })();
181
- </script>
182
- """,
183
- unsafe_allow_html=True,
184
- )
 
4
  from app.db import supabase
5
 
6
 
7
+ def main():
8
+ """Dedicated reset password page."""
9
+
10
+ st.set_page_config(
11
+ page_title="Reset Password - PNP Bot",
12
+ page_icon="assets/favicon.ico",
13
+ )
14
 
15
  # Center the content
16
  left, center, right = st.columns([1, 2, 1])
 
53
  st.info("Silakan kembali ke halaman login dan minta link reset password yang baru.")
54
 
55
  if st.button("← Kembali ke Login", type="primary"):
56
+ st.switch_page("app.py")
 
 
 
 
 
 
 
 
57
  return
58
 
59
  # Reset password form
 
80
  cancel_reset = st.form_submit_button("❌ Batal", use_container_width=True)
81
 
82
  if cancel_reset:
83
+ st.switch_page("app.py")
 
 
 
 
 
 
 
 
84
 
85
  if submit_reset:
86
  # Validation
 
109
  st.success("βœ… Password berhasil diubah!")
110
  st.info("Silakan login dengan password baru Anda.")
111
 
112
+ # Show countdown and redirect button
113
+ st.markdown("*Klik tombol di bawah untuk kembali ke halaman login.*")
 
 
 
 
 
 
 
 
 
114
 
115
+ if st.button("🏠 Kembali ke Login", type="primary"):
116
+ st.switch_page("app.py")
117
  else:
118
  st.error("❌ Gagal mengubah password. Silakan coba lagi.")
119
 
 
122
  st.info("Link mungkin sudah kedaluwarsa. Silakan minta link reset password yang baru.")
123
 
124
 
125
+ if __name__ == "__main__":
126
+ main()