Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django rest framework - adding to views.obtain_auth_token

I have implemented Token Authentication with django rest framework and I can post username and password to /api-token-auth/ and get the token.

url(r'^api-token-auth/', token_views.obtain_auth_token)

In addition to the token, I want to get the User object related to the returned token.

How can I override/add to this view and also return the actual User object?

like image 270
Ofek Agmon Avatar asked Jun 10 '15 19:06

Ofek Agmon


1 Answers

You can find the relevant view here:

https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/authtoken/views.py#L21

Assuming you've created some sort of User serializer already, you can basically take the user instance there and shove it into your UserSerializer. then add it to the response, something like the below.

... 
user_serializer = UserSerializer(user)
return Response({'token': token.key, 'user': user_serializer.data})
like image 83
Alex T Avatar answered Sep 27 '22 22:09

Alex T