switch to flask_login, finish login implementation, add basic home page

This commit is contained in:
2026-02-24 17:34:55 +01:00
parent 6d53b40118
commit 3970507239
5 changed files with 67 additions and 6 deletions
+12 -4
View File
@@ -1,7 +1,10 @@
from flask import Blueprint, render_template, request, redirect, url_for, session, flash
from flask_login import login_user
from app.db import get_db
from werkzeug.security import generate_password_hash, check_password_hash
from app.models.user import User
auth_bp = Blueprint("auth", __name__)
@auth_bp.route("/login", methods=["GET", "POST"])
@@ -11,11 +14,16 @@ def login():
password = request.form["password"]
db = get_db()
user = db.execute("SELECT * FROM users WHERE username = ?", (username,)).fetchone()
row = db.execute(
"SELECT * FROM users WHERE username = ?",
(username,)
).fetchone()
if user and check_password_hash(user["password"], password):
session["user_id"] = user["id"]
return redirect(url_for("main.main"))
if row and check_password_hash(row["password"], password):
user = User(id=row["id"], username=row["username"])
login_user(user)
return redirect(url_for("main.home"))
else:
flash("Invalid username or password")
+16 -1
View File
@@ -1,7 +1,22 @@
from flask import Blueprint, render_template
from flask_login import login_required, current_user
main_bp = Blueprint("main", __name__)
""" def login_required(view):
@wraps(view)
def wrapped_view(**kwargs):
if "user_id" not in session:
return redirect(url_for("auth.login"))
return view(**kwargs)
return wrapped_view
"""
@main_bp.route("/", methods=["GET", "POST"])
def main():
return render_template("main.html")
return render_template("main.html")
@main_bp.route("/home", methods=["GET", "POST"])
@login_required
def home():
return render_template("home.html", username=current_user.username)