Skip to content

Commit ef56efc

Browse files
committed
Merge pull request #152 from caruccio/logout-on-get
Allow logout on GET
2 parents 837d9a6 + 70a4dc9 commit ef56efc

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
- token
1719

rest_auth/tests/test_api.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,29 @@ def test_registration_with_email_verification(self):
378378
# try to login again
379379
self._login()
380380
self._logout()
381+
382+
@override_settings(ACCOUNT_LOGOUT_ON_GET=True)
383+
def test_logout_on_get(self):
384+
payload = {
385+
"username": self.USERNAME,
386+
"password": self.PASS
387+
}
388+
389+
# create user
390+
user = get_user_model().objects.create_user(self.USERNAME, '', self.PASS)
391+
392+
self.post(self.login_url, data=payload, status_code=200)
393+
self.get(self.logout_url, status=status.HTTP_200_OK)
394+
395+
@override_settings(ACCOUNT_LOGOUT_ON_GET=False)
396+
def test_logout_on_post_only(self):
397+
payload = {
398+
"username": self.USERNAME,
399+
"password": self.PASS
400+
}
401+
402+
# create user
403+
user = get_user_model().objects.create_user(self.USERNAME, '', self.PASS)
404+
405+
self.post(self.login_url, data=payload, status_code=status.HTTP_200_OK)
406+
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
@@ -10,6 +10,8 @@
1010
from rest_framework.permissions import IsAuthenticated, AllowAny
1111
from rest_framework.generics import RetrieveUpdateAPIView
1212

13+
from allauth.account import app_settings as allauth_settings
14+
1315
from .app_settings import (
1416
TokenSerializer, UserDetailsSerializer, LoginSerializer,
1517
PasswordResetSerializer, PasswordResetConfirmSerializer,
@@ -62,7 +64,23 @@ class LogoutView(APIView):
6264
"""
6365
permission_classes = (AllowAny,)
6466

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

0 commit comments

Comments
 (0)