1 / src /utils /auth.py
jayanth-2005's picture
Update src/utils/auth.py
640fb80 verified
Raw History Blame Contribute Delete
1.19 kB
import sqlite3
import hashlib
import os
DB_PATH = os.path.join(os.path.dirname(__file__), "..", "bharat_play.db") # Bundled DB path
def init_db():
pass # DB is prebuilt and bundled
def get_connection():
return sqlite3.connect(DB_PATH)
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()
def login_user(username, password):
conn = get_connection()
try:
c = conn.cursor()
hashed_pw = hash_password(password)
c.execute('SELECT * FROM users WHERE username=? AND password=?',
(username, hashed_pw))
return c.fetchone() is not None
finally:
conn.close()
def signup_user(username, password):
if not username or not password:
return False
conn = get_connection()
try:
c = conn.cursor()
hashed_pw = hash_password(password)
c.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, hashed_pw))
conn.commit()
return True
except sqlite3.IntegrityError:
return False
except Exception as e:
print(f"Error during signup: {e}")
return False
finally:
conn.close()