From 8a227ac60e6fbb930bbf16c44edec94405cbc30b Mon Sep 17 00:00:00 2001 From: Joel Whitaker Date: Thu, 29 Oct 2020 14:52:30 +0000 Subject: [PATCH] Add JWTSerializerWithExpiration so the expiration dates are serialized correctly --- dj_rest_auth/serializers.py | 5 +++++ dj_rest_auth/views.py | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/dj_rest_auth/serializers.py b/dj_rest_auth/serializers.py index f3a9614..57c375d 100644 --- a/dj_rest_auth/serializers.py +++ b/dj_rest_auth/serializers.py @@ -178,6 +178,11 @@ class JWTSerializer(serializers.Serializer): return user_data +class JWTSerializerWithExpiration(JWTSerializer): + access_token_expiration = serializers.DateTimeField() + refresh_token_expiration = serializers.DateTimeField() + + class PasswordResetSerializer(serializers.Serializer): """ Serializer for requesting a password reset e-mail. diff --git a/dj_rest_auth/views.py b/dj_rest_auth/views.py index 2f50379..366118c 100644 --- a/dj_rest_auth/views.py +++ b/dj_rest_auth/views.py @@ -12,6 +12,7 @@ from rest_framework.permissions import AllowAny, IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView +from serializers import JWTSerializerWithExpiration from .app_settings import (JWTSerializer, LoginSerializer, PasswordChangeSerializer, PasswordResetConfirmSerializer, @@ -51,7 +52,12 @@ class LoginView(GenericAPIView): def get_response_serializer(self): if getattr(settings, 'REST_USE_JWT', False): - response_serializer = JWTSerializer + + if getattr(settings, 'JWT_AUTH_RETURN_EXPIRATION', False): + response_serializer = JWTSerializerWithExpiration + else: + response_serializer = JWTSerializer + else: response_serializer = TokenSerializer return response_serializer