Spaces:
Sleeping
Sleeping
Delete pages/AUTH.py
Browse files- pages/AUTH.py +0 -82
pages/AUTH.py
DELETED
|
@@ -1,82 +0,0 @@
|
|
| 1 |
-
# auth.py
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import hashlib
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# If you prefer the helper, we show login widget here
|
| 7 |
-
succeeded = login_ui()
|
| 8 |
-
if succeeded:
|
| 9 |
-
# after successful login we set the flag and redirect to the home page
|
| 10 |
-
set_logged_in(True)
|
| 11 |
-
# redirect to the page you want as "home" (exact page title)
|
| 12 |
-
st.experimental_set_query_params(page="HOW TO USE") # change to your home page title
|
| 13 |
-
st.experimental_rerun()
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# ---------------- CONFIG ----------------
|
| 17 |
-
# This helper expects APP_PASSWORD to be defined in st.secrets
|
| 18 |
-
# Optionally set ALLOWED_USERS (info-only)
|
| 19 |
-
def _get_password_hash():
|
| 20 |
-
raw = st.secrets.get("APP_PASSWORD", None)
|
| 21 |
-
if not raw:
|
| 22 |
-
return None
|
| 23 |
-
return hashlib.sha256(raw.encode("utf-8")).hexdigest()
|
| 24 |
-
|
| 25 |
-
PASSWORD_HASH = _get_password_hash()
|
| 26 |
-
|
| 27 |
-
# ---------------- Session helpers ----------------
|
| 28 |
-
def is_logged_in() -> bool:
|
| 29 |
-
return bool(st.session_state.get("_logged_in", False))
|
| 30 |
-
|
| 31 |
-
def set_logged_in(value: bool = True):
|
| 32 |
-
st.session_state["_logged_in"] = bool(value)
|
| 33 |
-
|
| 34 |
-
def logout():
|
| 35 |
-
st.session_state["_logged_in"] = False
|
| 36 |
-
|
| 37 |
-
# ---------------- Login widget (use in AUTH page) ----------------
|
| 38 |
-
def login_ui():
|
| 39 |
-
"""Return True only when login succeeded (use in AUTH page)."""
|
| 40 |
-
st.title("🔒 Authentication")
|
| 41 |
-
st.markdown("Enter the app password to continue.")
|
| 42 |
-
pwd = st.text_input("Password", type="password", key="_pwd_input")
|
| 43 |
-
if st.button("Login", key="_login_btn"):
|
| 44 |
-
if PASSWORD_HASH is None:
|
| 45 |
-
st.error("No APP_PASSWORD defined in Secrets. Contact admin.")
|
| 46 |
-
return False
|
| 47 |
-
entered_hash = hashlib.sha256(pwd.encode("utf-8")).hexdigest()
|
| 48 |
-
if entered_hash == PASSWORD_HASH:
|
| 49 |
-
set_logged_in(True)
|
| 50 |
-
st.success("Login successful — redirecting...")
|
| 51 |
-
return True
|
| 52 |
-
else:
|
| 53 |
-
st.error("Incorrect password")
|
| 54 |
-
return False
|
| 55 |
-
return False
|
| 56 |
-
|
| 57 |
-
# ---------------- Enforcement helper ----------------
|
| 58 |
-
def require_login(auth_page_name: str = "AUTH", redirect_page: str = None):
|
| 59 |
-
"""
|
| 60 |
-
Call at top of every page. If user is not logged in:
|
| 61 |
-
- redirect user to the auth page (so they see login) and stop rendering.
|
| 62 |
-
auth_page_name : the exact page title used for your auth page in the multipage list.
|
| 63 |
-
redirect_page : optional page name to navigate to after successful login.
|
| 64 |
-
"""
|
| 65 |
-
# ensure session_state key exists
|
| 66 |
-
if "_logged_in" not in st.session_state:
|
| 67 |
-
st.session_state["_logged_in"] = False
|
| 68 |
-
|
| 69 |
-
# If user already logged in, do nothing
|
| 70 |
-
if is_logged_in():
|
| 71 |
-
return
|
| 72 |
-
|
| 73 |
-
# If current page is already the AUTH page, do not redirect (so auth page can render).
|
| 74 |
-
params = st.experimental_get_query_params()
|
| 75 |
-
cur_page = params.get("page", [None])[0]
|
| 76 |
-
# If cur_page equal to auth_page_name, allow rendering (auth page will present login)
|
| 77 |
-
if cur_page == auth_page_name or cur_page is None and st.title == auth_page_name:
|
| 78 |
-
return
|
| 79 |
-
|
| 80 |
-
# Otherwise redirect to auth page and stop rendering
|
| 81 |
-
st.experimental_set_query_params(page=auth_page_name)
|
| 82 |
-
st.stop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|