Spaces:
Sleeping
Sleeping
Download src/auth.py from jayanth-2005/BharatPlay: direct link, hf CLI and curl.
- Browser
- Download file 1.5 kB
-
https://huggingface.co/spaces/jayanth-2005/BharatPlay/resolve/main/src/auth.py
- Command line
-
hf download hf://spaces/jayanth-2005/BharatPlay/src/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/jayanth-2005/BharatPlay/resolve/main/src/auth.py
1.5 kB
| # ✅ auth.py (Updated with persistent login tracking) | |
| import sqlite3 | |
| import hashlib | |
| import os | |
| import tempfile | |
| # Persistent DB and login state file | |
| DB_PATH = os.path.join(tempfile.gettempdir(), "users.db") | |
| LOGIN_STATE_FILE = os.path.join(tempfile.gettempdir(), "last_login.txt") | |
| def get_connection(): | |
| conn = sqlite3.connect(DB_PATH) | |
| conn.execute(""" | |
| CREATE TABLE IF NOT EXISTS users ( | |
| username TEXT PRIMARY KEY, | |
| password TEXT | |
| ) | |
| """) | |
| return conn | |
| def hash_password(password): | |
| return hashlib.sha256(password.encode()).hexdigest() | |
| def signup_user(username, password): | |
| conn = get_connection() | |
| try: | |
| conn.execute("INSERT INTO users VALUES (?, ?)", (username, hash_password(password))) | |
| conn.commit() | |
| return True | |
| except sqlite3.IntegrityError: | |
| return False | |
| finally: | |
| conn.close() | |
| def login_user(username, password): | |
| conn = get_connection() | |
| hashed = hash_password(password) | |
| result = conn.execute("SELECT * FROM users WHERE username=? AND password=?", (username, hashed)).fetchone() | |
| conn.close() | |
| return result is not None | |
| def save_login(username): | |
| with open(LOGIN_STATE_FILE, "w") as f: | |
| f.write(username) | |
| def load_login(): | |
| if os.path.exists(LOGIN_STATE_FILE): | |
| with open(LOGIN_STATE_FILE, "r") as f: | |
| return f.read().strip() | |
| return None | |
| def logout_user(): | |
| if os.path.exists(LOGIN_STATE_FILE): | |
| os.remove(LOGIN_STATE_FILE) | |