Spaces:
Running
Running
Update app.py
Browse files3 unmodified lines456778910111011122 unmodified lines15161720181920212225232425282627302829303132333634353637403839404142434445464748484950511 unmodified line53545556565758596061586263646562666764686970713 unmodified lines
app = Flask(__name__)
# Configure SQLite database# Configure the SQLite databaseapp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Initialize the databasedb = SQLAlchemy(app)
# Define the User model2 unmodified lines username = db.Column(db.String(80), unique=True, nullable=False) password = db.Column(db.String(200), nullable=False)
# Create the database tables# Create the database and tables@app.before_first_requestdef create_tables(): db.create_all()
# Route for landing page# Landing page route@app.route('/')def landing_page(): return render_template('index.html') return render_template('index.html') # Ensure index.html exists in the templates folder
# Route for user registration# User registration route@app.route('/register', methods=['POST'])def register(): username = request.form.get('username') password = request.form.get('password')
# Check if the user already exists # Check if the username already exists if User.query.filter_by(username=username).first(): return "Username already exists. Please choose another."
# Hash the password and save the user to the database # Hash the password hashed_password = generate_password_hash(password)
# Save the user to the database new_user = User(username=username, password=hashed_password) db.session.add(new_user) db.session.commit()
return "User registered successfully!"
# Route for login# User login route@app.route('/login', methods=['POST'])def login(): username = request.form.get('username')1 unmodified line
# Look up the user in the database user = User.query.filter_by(username=username).first() if user and check_password_hash(user.password, password): # Check hashed password if not user: return "Invalid username."
# Verify the password if check_password_hash(user.password, password): response = make_response(redirect(url_for('landing_page'))) response.set_cookie('logged_in', 'true', max_age=3600) response.set_cookie('logged_in', 'true', max_age=3600) # Set session cookie for 1 hour response.set_cookie('username', username, max_age=3600) return response
return "Invalid username or password!" return "Invalid password!"
# Route for logout# User logout route@app.route('/logout')def logout(): response = make_response(redirect(url_for('landing_page')))app.py+18-14