Spaces:
Sleeping
Sleeping
| # utils/user_auth.py | |
| """User authentication module""" | |
| import streamlit as st | |
| import hashlib | |
| import json | |
| import os | |
| from datetime import datetime | |
| # Simple authentication system for demo | |
| # In production, use proper authentication libraries | |
| def hash_password(password: str) -> str: | |
| """Hash password using SHA256""" | |
| return hashlib.sha256(password.encode()).hexdigest() | |
| def load_users(): | |
| """Load users from file""" | |
| users_file = "data/users.json" | |
| if os.path.exists(users_file): | |
| with open(users_file, "r") as f: | |
| return json.load(f) | |
| return {} | |
| def save_users(users): | |
| """Save users to file""" | |
| os.makedirs("data", exist_ok=True) | |
| with open("data/users.json", "w") as f: | |
| json.dump(users, f, indent=2) | |
| def init_authentication(): | |
| """Initialize authentication in session state""" | |
| if "authenticated" not in st.session_state: | |
| st.session_state.authenticated = False | |
| if "username" not in st.session_state: | |
| st.session_state.username = None | |
| def authenticate_user(): | |
| """Show authentication UI and handle login/signup""" | |
| if st.session_state.authenticated: | |
| return True | |
| st.title("🎯 Welcome to TriviaVerse") | |
| st.markdown("### Please login or create an account to continue") | |
| tab1, tab2 = st.tabs(["Login", "Sign Up"]) | |
| with tab1: | |
| with st.form("login_form"): | |
| username = st.text_input("Username") | |
| password = st.text_input("Password", type="password") | |
| submitted = st.form_submit_button("Login", type="primary") | |
| if submitted: | |
| users = load_users() | |
| if username in users: | |
| if users[username]["password"] == hash_password(password): | |
| st.session_state.authenticated = True | |
| st.session_state.username = username | |
| st.session_state.user_id = username | |
| # Update last login | |
| users[username]["last_login"] = datetime.now().isoformat() | |
| save_users(users) | |
| st.success("Login successful!") | |
| st.rerun() | |
| else: | |
| st.error("Invalid password") | |
| else: | |
| st.error("Username not found") | |
| with tab2: | |
| with st.form("signup_form"): | |
| new_username = st.text_input("Choose a username") | |
| new_password = st.text_input("Choose a password", type="password") | |
| confirm_password = st.text_input("Confirm password", type="password") | |
| email = st.text_input("Email (optional)") | |
| terms = st.checkbox("I agree to the terms and conditions") | |
| submitted = st.form_submit_button("Sign Up", type="primary") | |
| if submitted: | |
| if not new_username or not new_password: | |
| st.error("Username and password are required") | |
| elif new_password != confirm_password: | |
| st.error("Passwords do not match") | |
| elif not terms: | |
| st.error("Please agree to the terms and conditions") | |
| else: | |
| users = load_users() | |
| if new_username in users: | |
| st.error("Username already exists") | |
| else: | |
| # Create new user | |
| users[new_username] = { | |
| "password": hash_password(new_password), | |
| "email": email, | |
| "created": datetime.now().isoformat(), | |
| "last_login": datetime.now().isoformat(), | |
| } | |
| save_users(users) | |
| # Auto login | |
| st.session_state.authenticated = True | |
| st.session_state.username = new_username | |
| st.session_state.user_id = new_username | |
| st.success("Account created successfully!") | |
| st.balloons() | |
| st.rerun() | |
| # Guest mode option | |
| st.divider() | |
| if st.button("Continue as Guest", type="secondary"): | |
| st.session_state.authenticated = True | |
| st.session_state.username = "Guest" | |
| st.session_state.user_id = f"guest_{int(datetime.now().timestamp())}" | |
| st.rerun() | |
| return False | |