I generate tokens using default view in Django:
url(r'^login/', rest_auth_views.obtain_auth_token),
I have a problem because my front-end doesn't know what is the currently logged in user ID.
Should I return it with token or maybe create some another request?
I know that there is a lot of different ways, but I would like to choose the most optimal solution.
I think the good practice will be to return user details in the response of login api.
If your built_in view doesn't return user details you can may be override the post method of
obtain_auth_token
. I once did this for djangorestframework-jwt obtain token methodby default
response_data
dict only hadtoken
details i added user object as well to achieve what you are trying to do.if you need to get user information on a webpage, you need to pass user information in a response of Login API or other API.
While using Token based authentication, after login, access token and refresh token are generated which are shall be given to client in login API response. This access token shall be passed in header as:
You need to put
authentication_classes = [OAuth2Authentication]
in your view.This will validate the if user is logged in also you will get to access logged in user's information by
user=request.user
.You could override
rest_framework.authtoken.views.ObtainAuthToken.post
in order to get the result you want.myapp/views.py
myapp/urls.py
Sample results
The idea here is to override the post method of the ObtainAuthToken view class. Here all I have done is call the parent class to get the token, then look up that token to find the associated user id.
Hope that helps.