0

need to catch the response messages of

@jwt_required()

inside the application

ex: if token not available or expire, I need to send a custom response to the client this is my sample code..


app = Flask(__name__)

app.config["JWT_SECRET_KEY"] = "super-secret"  # Change this!
jwt = JWTManager(app)


@app.route("/login", methods=["POST"])
def login():
    print("123")
    username = request.json.get("username", None)
    password = request.json.get("password", None)
    if username != "test" or password != "test":
        return jsonify({"msg": "Bad username or password"}), 401

    access_token = create_access_token(identity=username)
    return jsonify(access_token=access_token)


@app.route("/protected", methods=["GET"])
@jwt_required()
def protected():
    # Access the identity of the current user with get_jwt_identity
    current_user = get_jwt_identity()
    return jsonify(logged_in_as=current_user), 200


if __name__ == "__main__":
    app.run(debug=True)

if token not available out put


{ "msg": "Missing Authorization Header" }

but I need to send like this 


{ "status":"error",
"msg": "Missing Authorization Header" }

0 Answers0