0

I've been fiddling around with JWT_PAYLOAD_HANDLER trying to get a username returned in the output

So I tried this in my myApp/views.py:

def jwt_response_payload_handler(token, user=None, request=None):
    if user and request:
      return json.dumps({
        'token': token,
         'username':  str(request.user.username)
      })
    else:
      return { 'token': token }

and then I tried this in my project/settings.py:

JWT_AUTH = {
    'JWT_PAYLOAD_HANDLER':
    'myApp.views.jwt_response_payload_handler'
}

doing a curl request I get the error message:

TypeError at /api-token-auth/ <User: nunya> is not JSON serializable

I'm so close to figuring this out ... but I'm missing something.

What am I missing?

Simply Seth
  • 3,246
  • 17
  • 51
  • 77

1 Answers1

0

I finally managed to figure it out:

# myApp/views.py
def jwt_response_payload_handler(token, user=None, request=None):
  return { 'token': token, 'username': unicode(request.data['username']) }
Simply Seth
  • 3,246
  • 17
  • 51
  • 77