From 12e79aa33ebf835579492a1dd9db970aaef4fbdc Mon Sep 17 00:00:00 2001 From: alichass Date: Thu, 19 Mar 2020 14:37:35 -0400 Subject: [PATCH] changed for use w/ cookies --- dj_rest_auth/utils.py | 1 - dj_rest_auth/views.py | 13 +++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/dj_rest_auth/utils.py b/dj_rest_auth/utils.py index 165963d..85912a6 100644 --- a/dj_rest_auth/utils.py +++ b/dj_rest_auth/utils.py @@ -18,7 +18,6 @@ def default_create_token(token_model, user, serializer): def jwt_encode(user): try: from rest_framework_simplejwt.serializers import TokenObtainPairSerializer - from rest_framework_simplejwt.views import TokenObtainPairView except ImportError: raise ImportError("rest-framework-simplejwt needs to be installed") diff --git a/dj_rest_auth/views.py b/dj_rest_auth/views.py index f30980c..37e4880 100644 --- a/dj_rest_auth/views.py +++ b/dj_rest_auth/views.py @@ -83,6 +83,15 @@ class LoginView(GenericAPIView): context={'request': self.request}) response = Response(serializer.data, status=status.HTTP_200_OK) + if getattr(settings, 'REST_USE_JWT', False): + from rest_framework_simplejwt.settings import api_settings as jwt_settings + #if jwt_settings.JWT_AUTH_COOKIE #this needs to be added to simplejwt + from datetime import datetime + expiration = (datetime.utcnow() + jwt_settings.ACCESS_TOKEN_LIFETIME) + response.set_cookie('somestring', #replace with jwt_settings.JWT_AUTH_COOKIE + self.access_token, + expires=expiration, + httponly=True) return response def post(self, request, *args, **kwargs): @@ -125,6 +134,10 @@ class LogoutView(APIView): response = Response({"detail": _("Successfully logged out.")}, status=status.HTTP_200_OK) + if getattr(settings, 'REST_USE_JWT', False): + from rest_framework_simplejwt.settings import api_settings as jwt_settings + #if jwt_settings.JWT_AUTH_COOKIE #this needs to be added to simplejwt + response.delete_cookie('somestring') #replace with jwt_settings.JWT_AUTH_COOKIE return response