Skip to content

Commit

Permalink
fix: override logout view
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jun 19, 2024
1 parent 06bbc5b commit 1e4855c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
5 changes: 2 additions & 3 deletions codeforlife/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import typing as t

from django.contrib import admin
from django.contrib.auth.views import LogoutView
from django.http import HttpResponse
from django.urls import URLPattern, URLResolver, include, path, re_path
from rest_framework import status

from .settings import SERVICE_IS_ROOT, SERVICE_NAME
from .views import csrf
from .views import CsrfCookieView, LogoutView

UrlPatterns = t.List[t.Union[URLResolver, URLPattern]]

Expand All @@ -39,7 +38,7 @@ def get_urlpatterns(
),
path(
"api/csrf/cookie/",
csrf.CookieView.as_view(),
CsrfCookieView.as_view(),
name="get-csrf-cookie",
),
path(
Expand Down
2 changes: 1 addition & 1 deletion codeforlife/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"""

from .api import APIView
from .csrf import CookieView
from .common import CsrfCookieView, LogoutView
from .decorators import action, cron_job
from .model import ModelViewSet
11 changes: 10 additions & 1 deletion codeforlife/views/csrf.py → codeforlife/views/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Created on 12/04/2024 at 16:51:36(+01:00).
"""

from django.contrib.auth.views import LogoutView as _LogoutView
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import ensure_csrf_cookie
from rest_framework.request import Request
Expand All @@ -12,7 +14,7 @@
from ..permissions import AllowAny


class CookieView(APIView):
class CsrfCookieView(APIView):
"""A view to get a CSRF cookie."""

http_method_names = ["get"]
Expand All @@ -24,3 +26,10 @@ def get(self, request: Request):
Return a response which Django will auto-insert a CSRF cookie into.
"""
return Response()


class LogoutView(_LogoutView):
"""Override Django's logout view to always return a JSON response."""

def render_to_response(self, context, **response_kwargs):
return JsonResponse({})

0 comments on commit 1e4855c

Please sign in to comment.