1
File size: 1,190 Bytes
563b205
 
640fb80
a1e7ddf
640fb80
563b205
 
640fb80
8b45669
 
 
 
 
 
 
 
 
 
640fb80
8b45669
640fb80
 
 
8b45669
 
 
 
 
 
 
 
640fb80
8b45669
640fb80
8b45669
 
 
 
 
640fb80
8b45669
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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()