Where can I override jwt_response_payload_handler method? Where can I override jwt_response_payload_handler method? django django

Where can I override jwt_response_payload_handler method?


I found success doing the following:

myapp.view.py file:

def jwt_response_payload_handler(token, user=None, request=None):    return {        'token': token,        'bunny': 'fu fu'    }

setting.py file:

JWT_AUTH = {    'JWT_RESPONSE_PAYLOAD_HANDLER':    #'rest_framework_jwt.utils.jwt_response_payload_handler',    'myapp.views.jwt_response_payload_handler',}

Implementing the function jwt_response_payload_handler in an arbitrary location, but make sure it is in your python path. For example in this file: myapp.views.py

Then in your settings.py file update the JWT_AUTH dictionary key JWT_RESPONSE_PAYLOAD_HANDLER with the new location of the jwt_response_payload_handler you just created.

Once you grasp what's going on, you can adapt the solution how you would like. For example, I would not recommend leaving your overridden function in the views.py file. It was just simpler for demonstrating purposes.

Perhaps placing the jwt_response_payload_handler function in a "helper.py" file you create would be a simple solution.


jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLERjwt_encode_handler = api_settings.JWT_ENCODE_HANDLERpayload = jwt_payload_handler(user)token = jwt_encode_handler(payload)


Once you create your own handler method you'll have to change it in the JWT_AUTH setting. Check out the Additional Settings section in the docs.