I used to have this working:
auth.py
bp = Blueprint("auth", __name__)
@bp.before_app_request
def validate_token():
# code
session["user"] = user
index.py
@bp.route("/user")
def get_user():
return make_response(session["user"], 200)
However, after implementing tests to the application, before_app_request was being called and since the testing agent is not authenticated (nor should they be), it'd break. Changing to before_request seemed to fix the problem. In fact, it works fine when I run it with flask --app app_name run --debug. However, I deploy it to production using waitress. Running waitress-serve --port 8081 --host 0.0.0.0 --call app_name:create_app, the before_request is never called, resulting in an error.
For future reference:
before_requestis called before each request within the blueprint. To solve my error I had to keep thebefore_app_requestand check if on the app config "testing" wastrue:Not pretty but got the job done.