diff --git a/backend/api/authentication/__init__.py b/backend/api/authentication/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/api/authentication/apps.py b/backend/api/authentication/apps.py new file mode 100644 index 0000000..21468a0 --- /dev/null +++ b/backend/api/authentication/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ApiConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "api.authentication" diff --git a/backend/api/authentication/urls.py b/backend/api/authentication/urls.py new file mode 100644 index 0000000..12b7dae --- /dev/null +++ b/backend/api/authentication/urls.py @@ -0,0 +1,14 @@ +from django.urls import path + +from rest_framework_simplejwt.views import ( + TokenObtainPairView, + TokenRefreshView, +) + +from api.authentication.views import LogoutView + +urlpatterns = [ + path("logout/", LogoutView.as_view(), name="logout"), + path("token/", TokenObtainPairView.as_view(), name="token_obtain_pair"), + path("token/refresh/", TokenRefreshView.as_view(), name="token_refresh"), +] diff --git a/backend/api/authentication/views.py b/backend/api/authentication/views.py new file mode 100644 index 0000000..10fbcd5 --- /dev/null +++ b/backend/api/authentication/views.py @@ -0,0 +1,18 @@ +from rest_framework.response import Response +from rest_framework.views import APIView +from rest_framework.permissions import IsAuthenticated +from rest_framework_simplejwt.tokens import RefreshToken + + +# 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) diff --git a/backend/api/urls.py b/backend/api/urls.py index e8d5d5a..3f51d76 100644 --- a/backend/api/urls.py +++ b/backend/api/urls.py @@ -1,12 +1,6 @@ from django.urls import path, include -from rest_framework_simplejwt.views import ( - TokenObtainPairView, - TokenRefreshView, -) urlpatterns = [ - path("api/user/", include("api.user.urls")), - path("api/token/", TokenObtainPairView.as_view(), name="token_obtain_pair"), - path("api/token/refresh/", TokenRefreshView.as_view(), name="token_refresh"), + path("auth/", include("api.authentication.urls")), ]