Spaces:
Running
Running
Update app.py
Browse files12123445678910111213141516171819202122232425262711122829303132331434from dotenv import load_dotenvimport osimport sqlite3from werkzeug.security import generate_password_hash, check_password_hash
load_dotenv()# Create a database connectionconn = sqlite3.connect("users.db")cursor = conn.cursor()
# Create the users table (run this once)cursor.execute('''CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, username TEXT UNIQUE, password TEXT)''')conn.commit()
# Insert a user (run this once to populate the database)hashed_password = generate_password_hash("1234") # Hash the passwordcursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", ("admin", hashed_password))conn.commit()
@app.route('/login', methods=['POST'])def login(): username = request.form.get('username') password = request.form.get('password')
# Validate against environment variables if username == os.getenv("ADMIN_USERNAME") and password == os.getenv("ADMIN_PASSWORD"): # Check if the user exists in the database cursor.execute("SELECT password FROM users WHERE username = ?", (username,)) result = cursor.fetchone()
if result and check_password_hash(result[0], password): # Compare hashed passwords return "Login successful!" return "Invalid username or password!" return "Invalid username or password!"app.py+26-6