Flask web framework in Python for a simple login interface

Here is an example of a simple login app that uses Python and the Flask web framework to handle user authentication:
from flask import Flask, request, redirect, render_template
from flask_bcrypt import Bcrypt

app = Flask(__name__)
bcrypt = Bcrypt(app)

# create a dictionary to store our users' information
users = {
'username': bcrypt.generate_password_hash('password').decode('utf-8')

def index():
return render_template('index.html')

@app.route('/login', methods=['POST'])
def login():
username = request.form['username']
password = request.form['password']

if username in users:
if bcrypt.check_password_hash(users[username], password):
return redirect('/home')
return 'Invalid username or password'
return 'Invalid username or password'

def home():
return 'Welcome to the home page'

if __name__ == '__main__':

This code uses the Flask web framework to handle HTTP requests and responses, and the Flask-Bcrypt library to handle password hashing and checking. The users dictionary stores the usernames and hashed passwords of our users.

When the user navigates to the homepage, they will see the index.html template with a form where they can enter their username and password. When they submit the form, the /login endpoint will be called, and the app will check the username and password against the stored users. If the username and password match, the user will be redirected to the /home endpoint, where they will see a welcome message. If the username and password don't match, the user will see an "Invalid username or password" message.

Please note that this is a basic example, and in real-world app it's recommended to use a database to store the user's information, and use more sophisticated method to secure the login process.

Also this example has no logout functionality, and no session management, so it's important to implement those features as well to make a secure login app. try it!

Leave a Comment