# ✅ app.py(Updated with persistent login)
import os
os.environ["STREAMLIT_HOME"] = "/tmp/.streamlit"
import streamlit as st
from auth import login_user, signup_user, save_login, load_login, logout_user
st.set_page_config(page_title="🏸 BharatPlay", layout="wide")
# Restore login from file if exists
saved_user = load_login()
if saved_user and "authenticated" not in st.session_state:
st.session_state.authenticated = True
st.session_state.username = saved_user
# Session management defaults
if "authenticated" not in st.session_state:
st.session_state.authenticated = False
if "username" not in st.session_state:
st.session_state.username = None
# Header
st.markdown("""
🏸 BharatPlay
Explore & Contribute to Indian Games
""", unsafe_allow_html=True)
st.markdown("---")
# Login/signup form
if not st.session_state.authenticated:
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")
if st.form_submit_button("Login"):
if login_user(username, password):
st.session_state.authenticated = True
st.session_state.username = username
save_login(username)
st.success(f"✅ Welcome, {username}!")
st.rerun()
else:
st.error("❌ Invalid credentials.")
with tab2:
with st.form("signup_form"):
new_user = st.text_input("Choose Username")
new_pass = st.text_input("Choose Password", type="password")
if st.form_submit_button("Sign Up"):
if new_user and new_pass:
if signup_user(new_user, new_pass):
st.success("🎉 Account created! Please log in.")
else:
st.error("⚠️ Username already exists.")
else:
st.warning("Please fill both fields.")
st.stop()
# Sidebar account controls
with st.sidebar:
if st.session_state.authenticated:
st.markdown("### 🧑 Account")
st.success(f"✅ Logged in as `{st.session_state.username}`")
if st.button("🚪 Logout"):
logout_user()
st.session_state.authenticated = False
st.session_state.username = None
st.rerun()
# Game Categories UI
st.markdown("### 🎮 Choose a Game Category:")
categories = [
{"name": "Indoor", "icon": "🎲", "desc": "Games played indoors.", "page": "1_Indoor"},
{"name": "Outdoor", "icon": "🏏", "desc": "Games played outdoors.", "page": "2_Outdoor"},
{"name": "Traditional", "icon": "🪀", "desc": "Culturally rooted Indian games.", "page": "3_Traditional"},
{"name": "Martial Arts", "icon": "🥋", "desc": "Combat-based sports.", "page": "4_Martial_Arts"},
{"name": "Upload History", "icon": "📂", "desc": "View all uploads.", "page": "5_Upload_history"},
]
cols = st.columns(2)
for i, cat in enumerate(categories):
with cols[i % 2]:
with st.container():
st.markdown(f"""
{cat['icon']} {cat['name']}
{cat['desc']}
""", unsafe_allow_html=True)
# Explore button inside the box
st.page_link(f"pages/{cat['page']}.py", label=f"➡️ Explore {cat['name']}", use_container_width=True)
st.markdown("
", unsafe_allow_html=True)