jwt rest framework returning user id with token

2019-08-04 00:10发布

I'm using jwt-rest-framework for user authentication, and I would like to return a user_id with the token. There are some examples on how to do it when using Django-rest-framework, so according to this example I've tried to override ObtainJSONWenToken jwt view and did the same thing

from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from rest_framework_jwt.views import ObtainJSONWebToken

    # Create your views here.


    class CustomObtainJSONWebToken(ObtainJSONWebToken):
        """
        Return user id with token

        """
        def post(self, request, *args, **kwargs):
            response = super(CustomObtainJSONWebToken, self).post(request, *args, **kwargs)
            token = Token.objects.get(key=response.data['token'])
            return Response({'token': token.key, 'id': token.user_id})

But the Traceback is pointing out to the token it self:

Traceback:  

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  42.             response = get_response(request)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/lib/python3.5/contextlib.py" in inner
  30.                 return func(*args, **kwds)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
  489.             response = self.handle_exception(exc)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/rest_framework/views.py" in handle_exception
  449.             self.raise_uncaught_exception(exc)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
  486.             response = handler(request, *args, **kwargs)

File "/home/copser/Documents/iToucan-BackHand/iToucan/itoucan/rest_auth/views.py" in post
  15.         token = Token.objects.get(key=response.data['token'])

The question is what is the proper way of returning user_id with the token, how do I override this view so I can achieve this?

1条回答
相关推荐>>
2楼-- · 2019-08-04 00:28

You can add custom function for jwt payload response:

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

And in your django settings add:

JWT_AUTH = {
    ...
    'JWT_RESPONSE_PAYLOAD_HANDLER': 'your_path_to.jwt_response_payload_handler',
    ...
}
查看更多
登录 后发表回答