Search code examples
pythonreactjsdjangodjango-rest-auth

How to delete the Token from backend in Django


I'm using django rest-auth along with the django rest api. I can logout the user by calling rest-auth/logout in the browsable api and it will delete the token from the Database. In the browsable api, I don't have to send the Token along with the logout url. I called the same url rest-auth/logout from a React js front end and it gives the response as 'successfully logged out' but Token remains in the database. How can I remove the Token by calling the url from the front end.


Solution

  • You must send a DELETE request and use this code:

    class LogoutView(APIView):
    """ Logout User """
    
    @staticmethod
    def delete(request, *args, **kwargs):
        request.user.auth_token.delete()
        data = {
            "message": "You have successfully logged out.",
        }
        return Response(data, status=status.HTTP_200_OK)