jtvidela's picture
Update app.py
07d1ad9 verified
raw
history blame
1.64 kB
from dotenv import load_dotenv
import os
from flask import Flask, request, render_template, redirect, url_for, make_response
load_dotenv()
app = Flask(__name__)
@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"):
return "Login successful!"
return "Invalid username or password!"
# Hardcoded user database (replace with real database in production)
users = {
"admin": "1234",
"user1": "password"
}
@app.route('/')
def landing_page():
# Render the landing page HTML
return render_template('index.html') # Ensure this file is in the "templates" folder
@app.route('/login', methods=['POST'])
def login():
username = request.form.get('username')
password = request.form.get('password')
# Validate credentials
if username in users and users[username] == password:
response = make_response(redirect(url_for('landing_page')))
response.set_cookie('logged_in', 'true', max_age=3600) # Session cookie for 1 hour
response.set_cookie('username', username, max_age=3600)
return response
return "Invalid credentials. Please try again."
@app.route('/logout')
def logout():
response = make_response(redirect(url_for('landing_page')))
response.delete_cookie('logged_in')
response.delete_cookie('username')
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860) # Use port 7860 for Hugging Face Spaces