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__)

def _user_identity_lookup(user):
    return user.id

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)

@bp.route("/logout", methods=['POST'])
def logout():
    return {"status": "OK"}

jwt here is JWTManager, initialized with my app:

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

    with app.app_context():
        from . import login_bp

This code is basicly from the documentation examples:


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

Link to the repo:


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.

Answered By – vimalloc

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published