Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,73 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
from werkzeug.security import generate_password_hash, check_password_hash
|
| 3 |
|
| 4 |
-
|
| 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 |
-
#
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
if result and check_password_hash(result[0], password): # Compare hashed passwords
|
| 33 |
-
return "Login successful!"
|
| 34 |
return "Invalid username or password!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, redirect, url_for, make_response, render_template
|
| 2 |
+
from flask_sqlalchemy import SQLAlchemy
|
| 3 |
from werkzeug.security import generate_password_hash, check_password_hash
|
| 4 |
|
| 5 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Configure SQLite database
|
| 8 |
+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
|
| 9 |
+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 10 |
+
|
| 11 |
+
# Initialize the database
|
| 12 |
+
db = SQLAlchemy(app)
|
| 13 |
+
|
| 14 |
+
# Define the User model
|
| 15 |
+
class User(db.Model):
|
| 16 |
+
id = db.Column(db.Integer, primary_key=True)
|
| 17 |
+
username = db.Column(db.String(80), unique=True, nullable=False)
|
| 18 |
+
password = db.Column(db.String(200), nullable=False)
|
| 19 |
+
|
| 20 |
+
# Create the database tables
|
| 21 |
+
@app.before_first_request
|
| 22 |
+
def create_tables():
|
| 23 |
+
db.create_all()
|
| 24 |
+
|
| 25 |
+
# Route for landing page
|
| 26 |
+
@app.route('/')
|
| 27 |
+
def landing_page():
|
| 28 |
+
return render_template('index.html')
|
| 29 |
+
|
| 30 |
+
# Route for user registration
|
| 31 |
+
@app.route('/register', methods=['POST'])
|
| 32 |
+
def register():
|
| 33 |
+
username = request.form.get('username')
|
| 34 |
+
password = request.form.get('password')
|
| 35 |
+
|
| 36 |
+
# Check if the user already exists
|
| 37 |
+
if User.query.filter_by(username=username).first():
|
| 38 |
+
return "Username already exists. Please choose another."
|
| 39 |
+
|
| 40 |
+
# Hash the password and save the user to the database
|
| 41 |
+
hashed_password = generate_password_hash(password)
|
| 42 |
+
new_user = User(username=username, password=hashed_password)
|
| 43 |
+
db.session.add(new_user)
|
| 44 |
+
db.session.commit()
|
| 45 |
+
|
| 46 |
+
return "User registered successfully!"
|
| 47 |
+
|
| 48 |
+
# Route for login
|
| 49 |
@app.route('/login', methods=['POST'])
|
| 50 |
def login():
|
| 51 |
username = request.form.get('username')
|
| 52 |
password = request.form.get('password')
|
| 53 |
|
| 54 |
+
# Look up the user in the database
|
| 55 |
+
user = User.query.filter_by(username=username).first()
|
| 56 |
+
if user and check_password_hash(user.password, password): # Check hashed password
|
| 57 |
+
response = make_response(redirect(url_for('landing_page')))
|
| 58 |
+
response.set_cookie('logged_in', 'true', max_age=3600)
|
| 59 |
+
response.set_cookie('username', username, max_age=3600)
|
| 60 |
+
return response
|
| 61 |
|
|
|
|
|
|
|
| 62 |
return "Invalid username or password!"
|
| 63 |
+
|
| 64 |
+
# Route for logout
|
| 65 |
+
@app.route('/logout')
|
| 66 |
+
def logout():
|
| 67 |
+
response = make_response(redirect(url_for('landing_page')))
|
| 68 |
+
response.delete_cookie('logged_in')
|
| 69 |
+
response.delete_cookie('username')
|
| 70 |
+
return response
|
| 71 |
+
|
| 72 |
+
if __name__ == '__main__':
|
| 73 |
+
app.run(debug=True)
|