Skip to content

Instantly share code, notes, and snippets.

@dirkk0
Created May 31, 2024 09:07
Show Gist options
  • Save dirkk0/dc5981cf967ac8a14f2fc5006616039c to your computer and use it in GitHub Desktop.
Save dirkk0/dc5981cf967ac8a14f2fc5006616039c to your computer and use it in GitHub Desktop.
Minimal Flask-Login
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Home</title>
</head>
<body>
<nav>
<ul>
<li><a href="/login">Login</a></li>
<li><a href="/logout">Logout</a></li>
<li><a href="/register">Create account</a></li>
</ul>
</nav>
{% if current_user.is_authenticated %}
<h1>You are logged in</h1>
{% else %}
<h1>You are not logged in</h1>
{% endif %}
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Login</title>
<style>
h1{
color: green;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="/login">Login</a></li>
<li><a href="/register">Create account</a></li>
<li><a href="/logout">Logout</a></li>
</ul>
</nav>
<h1>Login to your account</h1>
<form action="#" method="post">
<label for="username">Username:</label>
<input type="text" name="username" />
<label for="password">Password:</label>
<input type="password" name="password" />
<button type="submit">Submit</button>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Login</title>
<style>
h1{
color: green;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="/login">Login</a></li>
<li><a href="/register">Create account</a></li>
<li><a href="/logout">Logout</a></li>
</ul>
</nav>
wrong password
<h1>Login to your account</h1>
<form action="#" method="post">
<label for="username">Username:</label>
<input type="text" name="username" />
<label for="password">Password:</label>
<input type="password" name="password" />
<button type="submit">Submit</button>
</form>
</body>
</html>
from flask import Flask, render_template, request, url_for, redirect
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, logout_user
from werkzeug.security import generate_password_hash, check_password_hash
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///db.sqlite"
app.config["SECRET_KEY"] = "abc"
db = SQLAlchemy()
login_manager = LoginManager()
login_manager.init_app(app)
class Users(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(250), unique=True, nullable=False)
password = db.Column(db.String(250), nullable=False)
db.init_app(app)
with app.app_context():
db.create_all()
@login_manager.user_loader
def loader_user(user_id):
return Users.query.get(user_id)
@app.route('/register', methods=["GET", "POST"])
def register():
username = request.form.get("username")
password = request.form.get("password")
if username:
# print(username)
# print(password)
password = generate_password_hash(
password, method='sha256') # scrypt, sha256
# print(password)
if request.method == "POST":
user = Users(username=username, password=password)
db.session.add(user)
db.session.commit()
return redirect(url_for("login"))
return render_template("sign_up.html")
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
user = Users.query.filter_by(username=username).first()
if user:
hash = user.password
c = check_password_hash(user.password, password)
if c:
login_user(user)
return redirect(url_for("home"))
else:
return render_template("login_wrong.html")
else:
return render_template("login_wrong.html")
return render_template("login.html")
@app.route("/logout")
def logout():
logout_user()
return redirect(url_for("home"))
@app.route("/")
def home():
return render_template("home.html")
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5001, debug=False)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Sign Up</title>
<style>
h1 {
color: green;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="/login">Login</a></li>
<li><a href="/register">Create account</a></li>
</ul>
</nav>
<h1>Create an account</h1>
<form action="#" method="post">
<label for="username">Username:</label>
<input type="text" name="username" />
<label for="password">Password:</label>
<input type="password" name="password" />
<button type="submit">Submit</button>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment