Skip to content

Commit

Permalink
Enable customizing login/logout responses
Browse files Browse the repository at this point in the history
  • Loading branch information
c-w committed Jan 16, 2023
1 parent 3a1bc58 commit 761dad2
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions knox/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def get_post_response_data(self, request, token, instance):
).data
return data

def get_post_response(self, request, token, instance):
data = self.get_post_response_data(request, token, instance)
return Response(data)

def post(self, request, format=None):
token_limit_per_user = self.get_token_limit_per_user()
if token_limit_per_user is not None:
Expand All @@ -71,19 +75,21 @@ def post(self, request, format=None):
instance, token = self.create_token()
user_logged_in.send(sender=request.user.__class__,
request=request, user=request.user)
data = self.get_post_response_data(request, token, instance)
return Response(data)
return self.get_post_response(request, token, instance)


class LogoutView(APIView):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)

def get_post_response(self, request):
return Response(None, status=status.HTTP_204_NO_CONTENT)

def post(self, request, format=None):
request._auth.delete()
user_logged_out.send(sender=request.user.__class__,
request=request, user=request.user)
return Response(None, status=status.HTTP_204_NO_CONTENT)
return self.get_post_response(request)


class LogoutAllView(APIView):
Expand All @@ -94,8 +100,11 @@ class LogoutAllView(APIView):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)

def get_post_response(self, request):
return Response(None, status=status.HTTP_204_NO_CONTENT)

def post(self, request, format=None):
request.user.auth_token_set.all().delete()
user_logged_out.send(sender=request.user.__class__,
request=request, user=request.user)
return Response(None, status=status.HTTP_204_NO_CONTENT)
return self.get_post_response(request)

0 comments on commit 761dad2

Please sign in to comment.