Search code examples
djangodjango-rest-frameworkdjango-viewsdjango-rest-framework-simplejwt

getting the current user with Django drf and simple jwt


I was trying to get the current user with rest framework and simple jwt

I'm new to Django and I wrote a simple view that does(ish) the job

  class UserDetail(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    authentication_classes = [JWTAuthentication,]
    permission_classes = [IsAuthenticated,]

but for some reason when I post the user credentials to the endpoint in postman it says

    {
    "detail": "Authentication credentials were not provided."
    }

but when I remove the ==> authentication_classes = [JWTAuthentication,] and permission_classes = [IsAuthenticated,]

it works and gite the current user, my question is is this the right way of doing it, and are there any risks in doing it this way and if their is a better and more secure way of doing it. thanks in advance!!

enter image description here this is the screenshot of the postM request data

and this is the URL for the class:-

path('detail/', UserDetail.as_view({'get':'list'}), name="blacklist"),

Solution

  • You have to pass the access token in the request. If you are not confident in setting up simplejwt, follow this guide:

    If you use postman to test the API, you can provide Authorization details like shown in this image

    If you are using any other medium to send requests, then you can add Authorization in the request's header: {"Authorization": "Bearer <access_token>"}