Where can I override the jwt_response_payload_handler method?

I installed JWT with pip. Now I want to override the method

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

be

 def jwt_response_payload_handler(token, user=None): return { 'token': token, 'user': UserSerializer(user).data } 

Where should I redefine it? I override the method in my application, but where and how? or change the source code in the library?

I changed the method in the library and it works fine, but I don't think this is the right way to do it. Can anybody help me? Thanks

+10
python django django-rest-framework jwt
source share
3 answers

Once you create your own handler method, you will have to change it in the JWT_AUTH setting. See the Advanced Settings section in the documents.

-one
source share

I found success by doing the following:

File myapp.view.py:

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

settings.py file:

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

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

Then, in the settings.py file, update the JWT_AUTH jwt_response_payload_handler dictionary key with the new location created by jwt_response_payload_handler .

Once you understand what is happening, you can adapt the solution as you want. For example, I would not recommend leaving an overridden function in the views.py file. It was just easier to demonstrate goals.

Placing the jwt_response_payload_handler function in your helper.py file might be a simple solution.

+20
source share
 jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) 
0
source share

All Articles