Skip to content

Commit 70a4dc9

Browse files
committed
Allow logout on GET
1 parent c087899 commit 70a4dc9

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

docs/api_endpoints.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ Basic
1111
- password (string)
1212

1313

14-
- /rest-auth/logout/ (POST)
14+
- /rest-auth/logout/ (POST, GET)
15+
16+
.. note:: ``ACCOUNT_LOGOUT_ON_GET = True`` to allow logout using GET (this is the exact same conf from allauth)
1517

1618
- /rest-auth/password/reset/ (POST)
1719

rest_auth/tests/test_api.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,29 @@ def test_registration_with_email_verification(self):
372372
# try to login again
373373
self._login()
374374
self._logout()
375+
376+
@override_settings(ACCOUNT_LOGOUT_ON_GET=True)
377+
def test_logout_on_get(self):
378+
payload = {
379+
"username": self.USERNAME,
380+
"password": self.PASS
381+
}
382+
383+
# create user
384+
user = get_user_model().objects.create_user(self.USERNAME, '', self.PASS)
385+
386+
self.post(self.login_url, data=payload, status_code=200)
387+
self.get(self.logout_url, status=status.HTTP_200_OK)
388+
389+
@override_settings(ACCOUNT_LOGOUT_ON_GET=False)
390+
def test_logout_on_post_only(self):
391+
payload = {
392+
"username": self.USERNAME,
393+
"password": self.PASS
394+
}
395+
396+
# create user
397+
user = get_user_model().objects.create_user(self.USERNAME, '', self.PASS)
398+
399+
self.post(self.login_url, data=payload, status_code=status.HTTP_200_OK)
400+
self.get(self.logout_url, status_code=status.HTTP_405_METHOD_NOT_ALLOWED)

rest_auth/views.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from rest_framework.permissions import IsAuthenticated, AllowAny
1010
from rest_framework.generics import RetrieveUpdateAPIView
1111

12+
from allauth.account import app_settings as allauth_settings
13+
1214
from .app_settings import (
1315
TokenSerializer, UserDetailsSerializer, LoginSerializer,
1416
PasswordResetSerializer, PasswordResetConfirmSerializer,
@@ -61,7 +63,23 @@ class LogoutView(APIView):
6163
"""
6264
permission_classes = (AllowAny,)
6365

66+
def get(self, request, *args, **kwargs):
67+
try:
68+
if allauth_settings.LOGOUT_ON_GET:
69+
response = self.logout(request)
70+
else:
71+
response = self.http_method_not_allowed(request, *args, **kwargs)
72+
except Exception as exc:
73+
response = self.handle_exception(exc)
74+
75+
return self.finalize_response(request, response, *args, **kwargs)
76+
self.response = self.finalize_response(request, response, *args, **kwargs)
77+
return self.response
78+
6479
def post(self, request):
80+
return self.logout(request)
81+
82+
def logout(self, request):
6583
try:
6684
request.user.auth_token.delete()
6785
except (AttributeError, ObjectDoesNotExist):

0 commit comments

Comments
 (0)