How do I get self.request.user in DRF from a request in vue.js?

how do I get self.request.user in DRF from a request in vue.js?
My request from .vue:

async LoadCase() {
    this.case = await fetch(
    `${this.$store.getters.getServerUrl}/casedetail/${this.slug}`
    ).then(response => response.json())
}

My class in views.py:

class DetailCase(RetrieveAPIView):
    queryset = Case.objects.all()
    serializer_class = CaseDetalSerializers
    lookup_field = "slug"

My class in serializers.py:

class CaseDetalSerializers(serializers.ModelSerializer):
    possible_open = serializers.SerializerMethodField()

    class Meta:
        model = Case
        exclude = ("id","slug")

    def get_possible_open(self,case):
        request = self.context.get('request')
        print(request)
        print(request.user)

What I get from print:

<rest_framework.request.Request: GET '/api/v1/casedetail/rofl'>
AnonymousUser

Right now I'm getting the AnonymousUser for some reason, and I should be getting admin. What do I have to do to get it?


#1 Answers

Hello you should use a method of authentication for your API in order to get a user otherwise it will be an AnonymousUser object.

This is a simple example of jwt authentication https://simpleisbetterthancomplex.com/tutorial/2018/12/19/how-to-use-jwt-authentication-with-django-rest-framework.html

Comments