From bf85a2373da08ee058576ae08e1b164dfa2f2df0 Mon Sep 17 00:00:00 2001 From: mateuvrs Date: Wed, 22 Nov 2023 00:33:51 -0300 Subject: [PATCH] users(views): create logout funct with simplejwt --- api/users/views.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/api/users/views.py b/api/users/views.py index 401acbf5..afac32ff 100644 --- a/api/users/views.py +++ b/api/users/views.py @@ -2,7 +2,7 @@ from rest_framework import exceptions from rest_framework.response import Response from rest_framework.request import Request -from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView +from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView, TokenBlacklistView from users.backends.utils import get_backend from users.simplejwt.decorators import move_refresh_token_to_cookie @@ -43,13 +43,27 @@ def post(self, request: Request, *args, **kwargs) -> Response: }, status.HTTP_400_BAD_REQUEST) -class RefreshJWTView(TokenRefreshView): +class HandleRefreshMixin: - @move_refresh_token_to_cookie - def post(self, request, *args, **kwargs): + def handle(self, request): try: request.data['refresh'] = request.COOKIES['refresh'] except KeyError: raise exceptions.NotAuthenticated('Refresh cookie error.') + return request + + +class RefreshJWTView(TokenRefreshView, HandleRefreshMixin): + + @move_refresh_token_to_cookie + def post(self, request, *args, **kwargs): + request = self.handle(request) + return super().post(request, *args, **kwargs) + + +class BlacklistJWTView(TokenBlacklistView, HandleRefreshMixin): + + def post(self, request, *args, **kwargs): + request = self.handle(request) return super().post(request, *args, **kwargs)