pythonflaskjwtflask-jwt-extended

How can I get current user's JWT informations?


I'm trying the get all orders that belonging the user that already login to the system.

I want to get the id information of the current user with the get() method below and get the order information belonging to current user from the order table. My goal is to get the current user's id from the JWT token using flask-jwt-extended.

How can I do that?

@api.route('/orders')
@jwt_required()
def get(self):
   # current_user_info
   user_id = current_user_info["id"]
   return UserService.get_orders(user_id)


Solution

  • flask_jwt_extended provides the get_jwt_identity() function, which returns the identity used to create the token used in the current call: create_access_token(identity=username).

    Link to the documentation

    So in your case, it should become something like this

    @api.route('/orders')
    @jwt_required()
    def get(self):
       # current_user_info
       user_id = get_jwt_identity()
       return UserService.get_orders(user_id)