Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I override jwt_response_payload_handler method?

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

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

to be

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

Where should I override it? Do I override the method in my own app, but where and how? or do I modify the original code in the library?

I have modified the method in the library and it works fine, but I don't think it is correct way to do. Can someone help me? Thanks

like image 853
song Avatar asked Apr 06 '15 00:04

song


1 Answers

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.

like image 80
aero Avatar answered Sep 21 '22 15:09

aero