Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,34 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
@app.route('/login', methods=['POST'])
|
| 7 |
def login():
|
| 8 |
username = request.form.get('username')
|
| 9 |
password = request.form.get('password')
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
return "Login successful!"
|
| 14 |
-
return "Invalid username or password!"
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
from werkzeug.security import generate_password_hash, check_password_hash
|
| 3 |
|
| 4 |
+
# Create a database connection
|
| 5 |
+
conn = sqlite3.connect("users.db")
|
| 6 |
+
cursor = conn.cursor()
|
| 7 |
+
|
| 8 |
+
# Create the users table (run this once)
|
| 9 |
+
cursor.execute('''
|
| 10 |
+
CREATE TABLE IF NOT EXISTS users (
|
| 11 |
+
id INTEGER PRIMARY KEY,
|
| 12 |
+
username TEXT UNIQUE,
|
| 13 |
+
password TEXT
|
| 14 |
+
)
|
| 15 |
+
''')
|
| 16 |
+
conn.commit()
|
| 17 |
+
|
| 18 |
+
# Insert a user (run this once to populate the database)
|
| 19 |
+
hashed_password = generate_password_hash("1234") # Hash the password
|
| 20 |
+
cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", ("admin", hashed_password))
|
| 21 |
+
conn.commit()
|
| 22 |
|
| 23 |
@app.route('/login', methods=['POST'])
|
| 24 |
def login():
|
| 25 |
username = request.form.get('username')
|
| 26 |
password = request.form.get('password')
|
| 27 |
|
| 28 |
+
# Check if the user exists in the database
|
| 29 |
+
cursor.execute("SELECT password FROM users WHERE username = ?", (username,))
|
| 30 |
+
result = cursor.fetchone()
|
| 31 |
+
|
| 32 |
+
if result and check_password_hash(result[0], password): # Compare hashed passwords
|
| 33 |
return "Login successful!"
|
| 34 |
+
return "Invalid username or password!"
|