how to validate the JWT token in Simple JWT
authentication. I need to verify when the user sends a request if the token access token is expired so it will redirect him to refresh the token.
serializers.py
class CustomTokenObtainPairSerializer(TokenObtainPairSerializer):
def validate(self, attrs):
# The default result (access/refresh tokens)
data = super(CustomTokenObtainPairSerializer, self).validate(attrs)
# Custom data you want to include
data.update({'email': self.user.email})
data.update({'id': self.user.id})
data.update({'status':"success"})
# and everything else you want to send in the response
return data
views.py
class CustomTokenObtainPairView(TokenObtainPairView):
# Replace the serializer with your custom
serializer_class = CustomTokenObtainPairSerializer
urls.py
from rest_framework_simplejwt.views import TokenRefreshView, TokenVerifyView
urlpatterns = [
path('token/', views.CustomTokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('token/verify/', TokenVerifyView.as_view(), name='token_verify'),
]
urlpatterns = format_suffix_patterns(urlpatterns)