1

I am trying to get current logged in user and upload/send information to frontend. I am new to Rest Framework so forgive my basic level question.

I have tried fetching user with request.user and request.auth in my views but I am not able to simply get user. This is my views.py

from rest_framework.authtoken.models import Token

def get_User (request):
    # current_user = request.user
    # context = {"user": current_user}
    # print(request.user.username)

    user_id = Token.objects.get(key=request.auth.key).user_id
    print(request.auth)

    # Response(context)

I am using a custom user model to store data.

Every time I am running request.auth or request.user i can see in my django terminal that it keeps going to /verify-auth-token/ URL.

How do I get user printed also sent to frontend in React to display in app as well. Any help is appreciated.

Edit 1: I am passing user from above mentioned view only.

from rest_framework.authtoken.models import Token
user_id = Token.objects.get(key=request.auth.key).user_id
user = Users.object.get(id =user_id)

Response (user)

This is how I think I should be passing variable to frontend but I am not getting anything achieved this way.

2 Answers 2

2

If you are working with DRF and you want to pass information to your frontend, you cannot use request.user since request is working with sessions.

To identify an user you need to associate a token with an user and then pass the associate object user to your frontend and the other way around so a quick example :

def get_user_by_token(token):
 user_id = Token.objects.get(key=request.auth.key).user_id
 user = Users.object.get(id =user_id)
 return JsonResponse({"user": user.id})

So you can return whatever you want in a JSON format to your frontend once the user is associated with the token

Sign up to request clarification or add additional context in comments.

3 Comments

You haven't defined request in example when i define request to function. It requires argument. How do get token key to verify. Thanks for providing insights to question
Where to your work with request ? Are you using DRF and React of Django with a template ?
Yes, I am using DRF. I am not using React-django template. My both apps are independent with API and Frontend. by request I meant in above example you wrote in answer has request.auth.user which doesn't work as there is no call to request.
1

First make sure you have SessionMiddleware and AuthenticationMiddleware middlewares added to your MIDDLEWARE_CLASSES setting.

The current user is in request object, you can get it by:

def sample_view(request): current_user = request.user print(current_user)

request.user will give you a User object representing the currently logged-in user. If a user isn't currently logged in, request.user will be set to an instance of AnonymousUser. You can tell them apart with the field is_authenticated, like so:

if request.user.is_authenticated: # Do something for authenticated users. else: # Do something for anonymous users.

Hope it works!!

Thank you!!

5 Comments

Thanks. I can get user with request.user but I can't pass it to react like {{request.user}} anyway i can pass this data to frontend without writing serializer. Thanks for responding.
This work only if you are using a session with a template or simple http page, in React and DRF you do not work with session but token authentication
@SahilMohile you need to pass the token object to authenticate the user, show us a view in which you are trying to pass the request user
@GaëtanGR I have updated question. Please check
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.