Search code examples
pythonflaskflask-jwt-extended

Flask app doen't register jwt.user_lookup_loader, Flask-JWT-Extended


I have a Flask app with blueprints. It worked just fine, but than I decided to use flask_jwt_extended to handle tokens. It is said in docs that I can decorate method with jwt.user_lookup_loader to have current_user working. But for some reason calling current_user ends up with an error:

You must provide a `@jwt.user_lookup_loader` callback to use this method

But it is there, in the same blueprint. There is also a method, decorated with @jwt.user_identity_loader and it works perfectly well. Here is a simplified version of my code:

from . import rpc, jwt
from flask_jwt_extended import current_user, jwt_required

bp = Blueprint('login_bp', __name__)

@jwt.user_identity_loader
def _user_identity_lookup(user):
    return user.id

@jwt.user_lookup_loader
def _user_lookup_callback(_jwt_header, jwt_data):
    identity = jwt_data["sub"]
    user = rpc.cache_service.get_user(identity)
    if user is None:
        return None
    return UserSchema().load(user)

@jwt_required()
@bp.route("/logout", methods=['POST'])
def logout():
    rpc.cache_service.forget_user(current_user.id)
    return {"status": "OK"}

jwt here is JWTManager, initialized with my app:

jwt = JWTManager()
def create_app():
    app = Flask(__name__, instance_relative_config=False)

    app.config.from_mapping(JWT_SECRET_KEY=os.environ["JWT_SECRET_KEY"])
...
    jwt.init_app(app)
...
    with app.app_context():
        from . import login_bp
        app.register_blueprint(login_bp.bp)

This code is basicly from the documentation examples:

https://flask-jwt-extended.readthedocs.io/en/stable/automatic_user_loading/

and I can't see what the problem might be (

Link to the repo: https://github.com/GreenBlackSky/COIN/blob/master/api_app/app/login_bp.py


Solution

  • Your decorators are in the wrong order. @bp.route() needs to come before @jwt_required(), otherwise it tries to evaluate the logout method when a request comes in before it decodes the JWT in the request.