From 13b44789118a5ed30d7f6a8658498eb8ea0e0fd7 Mon Sep 17 00:00:00 2001 From: Aroy-Art Date: Sun, 2 Feb 2025 23:45:29 +0100 Subject: [PATCH] Add jwt logout view --- backend/api/user/urls.py | 3 ++- backend/api/user/views.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/backend/api/user/urls.py b/backend/api/user/urls.py index 1c5510c..e109ee9 100644 --- a/backend/api/user/urls.py +++ b/backend/api/user/urls.py @@ -1,7 +1,8 @@ from django.urls import path -from .views import ProfileView +from .views import ProfileView, LogoutView urlpatterns = [ + path("logout/", LogoutView.as_view(), name="logout"), path("profile/", ProfileView.as_view(), name="profile"), ] diff --git a/backend/api/user/views.py b/backend/api/user/views.py index 5c30488..a5b684b 100644 --- a/backend/api/user/views.py +++ b/backend/api/user/views.py @@ -1,10 +1,25 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView +from rest_framework_simplejwt.tokens import RefreshToken from .serializers import UserSerializer +# Logout View +class LogoutView(APIView): + permission_classes = [IsAuthenticated] + + def post(self, request): + try: + refresh_token = request.data["refresh"] + token = RefreshToken(refresh_token) + token.blacklist() + return Response({"message": "Logout successful"}) + except Exception as e: + return Response({"error": str(e)}, status=400) + + class ProfileView(APIView): permission_classes = [IsAuthenticated]