changed for use w/ cookies

This commit is contained in:
alichass 2020-03-19 14:37:35 -04:00
parent 6dd2aea624
commit 12e79aa33e
2 changed files with 13 additions and 1 deletions

View File

@ -18,7 +18,6 @@ def default_create_token(token_model, user, serializer):
def jwt_encode(user): def jwt_encode(user):
try: try:
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from rest_framework_simplejwt.views import TokenObtainPairView
except ImportError: except ImportError:
raise ImportError("rest-framework-simplejwt needs to be installed") raise ImportError("rest-framework-simplejwt needs to be installed")

View File

@ -83,6 +83,15 @@ class LoginView(GenericAPIView):
context={'request': self.request}) context={'request': self.request})
response = Response(serializer.data, status=status.HTTP_200_OK) 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 return response
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
@ -125,6 +134,10 @@ class LogoutView(APIView):
response = Response({"detail": _("Successfully logged out.")}, response = Response({"detail": _("Successfully logged out.")},
status=status.HTTP_200_OK) 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 return response