Skip to content

Flask-Login Patching Issue #6

@kreathon

Description

@kreathon

It seems that the patching of Flask-Login does not work in all cases correctly.

The following is a minimal reproducible example where current_user is always None.

import quart_flask_patch  # noqa

import quart
import flask_login


login_manager = flask_login.LoginManager()


def create_app():
    app = quart.Quart(__name__)

    login_manager.init_app(app)

    @app.route("/")
    def hello():
        return "Hello, World!"

    @app.before_request
    def load_user():
 
        # For an explanation why this is interesting see below:
        import quart
        import flask
        print(quart.has_request_context())  #  <- True
        print(flask.has_request_context())  #  <- False

        assert flask_login.current_user is not None # <- This breaks

    return app

Dependencies:

Quart == 0.19.9
Flask-Login == 0.6.3
quart-flask-patch == 0.3.0

Implementation of current_user (in Flask-Login):

current_user = LocalProxy(lambda: _get_user())
...

def _get_user():
    if has_request_context():        #  <- this is ALWAYS false
        if "_login_user" not in g:
            current_app.login_manager._load_user()

        return g._login_user

    return None

The following ugly workaround seems to work:

import quart_flask_patch  # noqa

from quart import has_request_context as quart_has_request_context

import flask

flask.has_request_context = lambda: quart_has_request_context()

...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions