Get User Details using Header and Token
Opened this issue · 1 comments
Hello, I am in process of creating a Django REST API for a user authentication and authorization system using Django and Django REST Framework.
I am stuck on getting user information. Here is the conditions for it:
Method: GET
Header: Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxMjMsInVzZXJuYW1lIjoiZXhhbXBsZVVzZXIiLCJleHAiOjE3MTIxNDYxNDd9.zKobBlRuOiJSxCmi-iYap1bejfnvK6M3qtnkT0ssDKA
Response: {"id": 1, "username": "", "email": "user@example.com"}
Here's the curl:
curl -X GET http://localhost:8000/api/me/ -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxMjMsInVzZXJuYW1lIjoiZXhhbXBsZVVzZXIiLCJleHAiOjE3MTIxNDYxNDd9.zKobBlRuOiJSxCmi-iYap1bejfnvK6M3qtnkT0ssDKA"
I have been using the User model from django.contrib.auth.models.
Here's the serializer:
class UserSerializer(serializers.ModelSerializer):
acess_token = serializers.UUIDField()
class Meta:
model = User
fields = ["id", "username", "email"]
Here's the view:
class DumUserView(generics.RetrieveAPIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
queryset = User.objects.all()
serializer_class = UserSerializer
def get(self, request):
user = request.user
serializer = self.get_serializer(user)
return Response(serializer.data)
Url:
path('api/dumuser',DumUserView.as_view)
When I insert this command in the terminal,
curl -X GET http://127.0.0.1:8000/api/dumuser -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzE2NDg3OTU5LCJpYXQiOjE3MTY0ODc2NTksImp0aSI6IjM1N2Y2ZDJjOWM1MjQzZWY4OWRjZWZmZTA2ZjNjMTc0IiwidXNlcl9pZCI6MTYsIm5hbWUiOiJvbGVnLmsuYW5hbmlldkBnbWFpbC5jb20ifQ.nDEqHRUPYvj9ysYrKNoCYx_sPm_JdpXCcdvrImPjlTU"
I get a message Server Error 500:
<!doctype html>
<title>Server Error (500)</title>Server Error (500)
Can someone please tell me where I am doing it wrong and what will you recommend to do?
Thank you.
Hi, your question has a couple of things that don't make sense. If you view throws exceptions, this has nothing to do with our schema.
If you have a custom get_serializer
you might need to follow this for a sane fallback while generating a schema: https://drf-spectacular.readthedocs.io/en/latest/faq.html#my-get-queryset-depends-on-some-attributes-not-available-at-schema-generation-time