switch to flask_login, finish login implementation, add basic home page
This commit is contained in:
+12
-4
@@ -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")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user