Skip to content

Commit

Permalink
users(views): create logout funct with simplejwt
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusvrs committed Nov 22, 2023
1 parent eb158d3 commit bf85a23
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions api/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Check warning on line 68 in api/users/views.py

View check run for this annotation

Codecov / codecov/patch

api/users/views.py#L68

Added line #L68 was not covered by tests
return super().post(request, *args, **kwargs)

0 comments on commit bf85a23

Please sign in to comment.