I had the same problem. One way to solve this problem is to have new serializers for access and refresh APIs, like:
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer, TokenRefreshSerializer
from rest_framework_simplejwt.tokens import RefreshToken
class TokenObtainLifetimeSerializer(TokenObtainPairSerializer):
def validate(self, attrs):
data = super().validate(attrs)
refresh = self.get_token(self.user)
data['lifetime'] = int(refresh.access_token.lifetime.total_seconds())
return data
class TokenRefreshLifetimeSerializer(TokenRefreshSerializer):
def validate(self, attrs):
data = super().validate(attrs)
refresh = RefreshToken(attrs['refresh'])
data['lifetime'] = int(refresh.access_token.lifetime.total_seconds())
return data
And also add new views for that as well like:
from rest_framework_simplejwt.views import TokenViewBase
from accounts.api.serializers import TokenObtainLifetimeSerializer, TokenRefreshLifetimeSerializer
class TokenObtainPairView(TokenViewBase):
"""
Return JWT tokens (access and refresh) for specific user based on username and password.
"""
serializer_class = TokenObtainLifetimeSerializer
class TokenRefreshView(TokenViewBase):
"""
Renew tokens (access and refresh) with new expire time based on specific user's access token.
"""
serializer_class = TokenRefreshLifetimeSerializer
and finally add new views to your urls like:
from django.urls import path
from accounts.api.views import TokenObtainPairView, TokenRefreshView
urlpatterns = [
path('token/obtain/', TokenObtainPairView.as_view(), name='token-obtain'),
path('token/refresh/', TokenRefreshView.as_view(), name='token-refresh'),
]
Now despite of those tokens of yours, you also have expire times in both of your APIs.