mirror of
https://github.com/StefBuwalda/cal_counter.git
synced 2025-10-30 03:10:00 +00:00
Refactor login_required and add auth blueprint
Moved the login_required logic to a new utils.py for reuse. Added a new auth blueprint and registered it in app.py. Updated user blueprint to use the shared login_required function.
This commit is contained in:
2
app.py
2
app.py
@@ -17,6 +17,7 @@ from application import db, app, login_manager
|
|||||||
from application.admin.routes import admin_bp
|
from application.admin.routes import admin_bp
|
||||||
from application.user.routes import user_bp
|
from application.user.routes import user_bp
|
||||||
from application.add_meal.routes import bp as add_meal_bp
|
from application.add_meal.routes import bp as add_meal_bp
|
||||||
|
from application.auth.routes import bp as auth_bp
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
# Config
|
# Config
|
||||||
@@ -34,6 +35,7 @@ def load_user(user_id: int):
|
|||||||
app.register_blueprint(admin_bp)
|
app.register_blueprint(admin_bp)
|
||||||
app.register_blueprint(user_bp)
|
app.register_blueprint(user_bp)
|
||||||
app.register_blueprint(add_meal_bp)
|
app.register_blueprint(add_meal_bp)
|
||||||
|
app.register_blueprint(auth_bp)
|
||||||
|
|
||||||
|
|
||||||
# Routes
|
# Routes
|
||||||
|
|||||||
13
application/auth/routes.py
Normal file
13
application/auth/routes.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from flask import (
|
||||||
|
Blueprint,
|
||||||
|
)
|
||||||
|
from application.utils import login_required
|
||||||
|
|
||||||
|
bp = Blueprint(
|
||||||
|
"user",
|
||||||
|
__name__,
|
||||||
|
template_folder="templates",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
bp.before_request(login_required)
|
||||||
@@ -12,6 +12,7 @@ from application import db
|
|||||||
from forms import FoodItemForm
|
from forms import FoodItemForm
|
||||||
from models import FoodItem, FoodLog
|
from models import FoodItem, FoodLog
|
||||||
from datetime import datetime, timezone, timedelta
|
from datetime import datetime, timezone, timedelta
|
||||||
|
from application.utils import login_required
|
||||||
|
|
||||||
user_bp = Blueprint(
|
user_bp = Blueprint(
|
||||||
"user",
|
"user",
|
||||||
@@ -20,10 +21,7 @@ user_bp = Blueprint(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@user_bp.before_request
|
user_bp.before_request(login_required)
|
||||||
def login_required():
|
|
||||||
if not current_user.is_authenticated:
|
|
||||||
return redirect(url_for("login"))
|
|
||||||
|
|
||||||
|
|
||||||
@user_bp.route("/dashboard", methods=["GET"])
|
@user_bp.route("/dashboard", methods=["GET"])
|
||||||
|
|||||||
11
application/utils.py
Normal file
11
application/utils.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
from flask_login import current_user
|
||||||
|
from flask import redirect, url_for, flash
|
||||||
|
|
||||||
|
|
||||||
|
def login_required():
|
||||||
|
if not current_user.is_authenticated:
|
||||||
|
return redirect(url_for("auth.login"))
|
||||||
|
if current_user.must_change_password:
|
||||||
|
flash("You have to change your password")
|
||||||
|
return redirect(url_for("auth.change_password"))
|
||||||
|
return
|
||||||
Reference in New Issue
Block a user