Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,9 @@ 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
|
|
@@ -17,35 +15,37 @@ class User(db.Model):
|
|
| 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 |
-
#
|
| 26 |
@app.route('/')
|
| 27 |
def landing_page():
|
| 28 |
-
return render_template('index.html')
|
| 29 |
|
| 30 |
-
#
|
| 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
|
| 37 |
if User.query.filter_by(username=username).first():
|
| 38 |
return "Username already exists. Please choose another."
|
| 39 |
|
| 40 |
-
# Hash the password
|
| 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 |
-
#
|
| 49 |
@app.route('/login', methods=['POST'])
|
| 50 |
def login():
|
| 51 |
username = request.form.get('username')
|
|
@@ -53,15 +53,19 @@ def login():
|
|
| 53 |
|
| 54 |
# Look up the user in the database
|
| 55 |
user = User.query.filter_by(username=username).first()
|
| 56 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 63 |
|
| 64 |
-
#
|
| 65 |
@app.route('/logout')
|
| 66 |
def logout():
|
| 67 |
response = make_response(redirect(url_for('landing_page')))
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
+
# Configure the SQLite database
|
| 8 |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
|
| 9 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
|
|
|
| 10 |
db = SQLAlchemy(app)
|
| 11 |
|
| 12 |
# Define the User model
|
|
|
|
| 15 |
username = db.Column(db.String(80), unique=True, nullable=False)
|
| 16 |
password = db.Column(db.String(200), nullable=False)
|
| 17 |
|
| 18 |
+
# Create the database and tables
|
| 19 |
@app.before_first_request
|
| 20 |
def create_tables():
|
| 21 |
db.create_all()
|
| 22 |
|
| 23 |
+
# Landing page route
|
| 24 |
@app.route('/')
|
| 25 |
def landing_page():
|
| 26 |
+
return render_template('index.html') # Ensure index.html exists in the templates folder
|
| 27 |
|
| 28 |
+
# User registration route
|
| 29 |
@app.route('/register', methods=['POST'])
|
| 30 |
def register():
|
| 31 |
username = request.form.get('username')
|
| 32 |
password = request.form.get('password')
|
| 33 |
|
| 34 |
+
# Check if the username already exists
|
| 35 |
if User.query.filter_by(username=username).first():
|
| 36 |
return "Username already exists. Please choose another."
|
| 37 |
|
| 38 |
+
# Hash the password
|
| 39 |
hashed_password = generate_password_hash(password)
|
| 40 |
+
|
| 41 |
+
# Save the user to the database
|
| 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 |
+
# User login route
|
| 49 |
@app.route('/login', methods=['POST'])
|
| 50 |
def login():
|
| 51 |
username = request.form.get('username')
|
|
|
|
| 53 |
|
| 54 |
# Look up the user in the database
|
| 55 |
user = User.query.filter_by(username=username).first()
|
| 56 |
+
if not user:
|
| 57 |
+
return "Invalid username."
|
| 58 |
+
|
| 59 |
+
# Verify the password
|
| 60 |
+
if check_password_hash(user.password, password):
|
| 61 |
response = make_response(redirect(url_for('landing_page')))
|
| 62 |
+
response.set_cookie('logged_in', 'true', max_age=3600) # Set session cookie for 1 hour
|
| 63 |
response.set_cookie('username', username, max_age=3600)
|
| 64 |
return response
|
| 65 |
|
| 66 |
+
return "Invalid password!"
|
| 67 |
|
| 68 |
+
# User logout route
|
| 69 |
@app.route('/logout')
|
| 70 |
def logout():
|
| 71 |
response = make_response(redirect(url_for('landing_page')))
|