Files
flask_test/app.py
2023-04-26 18:56:48 +02:00

42 lines
1.2 KiB
Python

from flask import Flask, request, render_template
app = Flask(__name__)
# Define a dictionary with the valid usernames and passwords
users = {
'user1': 'password1',
'user2': 'password2'
}
@app.route('/', methods=['GET', 'POST'])
def login():
# Check if the user is already logged in
if 'username' in session:
return f'Logged in as {session["username"]}.'
# Check if the user submitted the login form
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
# Check if the username and password are valid
if username in users and users[username] == password:
# Store the username in the session
session['username'] = username
return f'Logged in as {username}.'
else:
return 'Invalid username or password.'
# Render the login form
return render_template('login.html')
@app.route('/logout')
def logout():
# Remove the username from the session if it exists
session.pop('username', None)
return 'Logged out.'
if __name__ == '__main__':
app.secret_key = 'supersecretkey'
app.run()