stackoverflow rohit
stackoverflow rohit

Reputation: 315

How to validate a token in Django Knox Authentication

I am using knox authentication to login user after validating otp using the view below

class LoginPhoneApi(KnoxLoginView):
    permission_classes = (permissions.AllowAny,)

    def post(self,request,formt=None):
        serializer = LoginwithPhoneSerializer(data = request.data)
        serializer.is_valid(raise_exception = True)
        user = serializer.validated_data['user']
        login(request,user,backend='django.contrib.auth.backends.ModelBackend')
        userid = user.id
        username = user.username
        fullname= user.full_name
        temp_list=super().post(request, format=None)

        temp_list.data["userid"]=userid
        temp_list.data["username"]=username
        temp_list.data["fullname"]=fullname
        print(temp_list.data["token"])
        return Response({"data":temp_list.data})

I am able to login and get the authentication token. Now one of my view requires authenticated users. How do I validate this token which is in the header of the url request.

I am able to fetch the token. How do I use this token to validate and login the user.

def dispatch(self, request, *args, **kwargs):
        self.quiz = get_object_or_404(Quiz, url=self.kwargs['quiz_name'])

        auth_method, token = self.request.META['HTTP_AUTHORIZATION'].split(' ', 1)

Please let me know if you need more information.

Upvotes: 0

Views: 1214

Answers (0)

Related Questions