Codementor Events

rest_framework_simplejwt modify access token expiry time based on user role

Published Dec 19, 2018

from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from rest_framework_simplejwt.views import TokenObtainPairView
from rest_framework_simplejwt.utils import datetime_to_epoch

SUPERUSER_LIFETIME = datetime.timedelta(minutes=1)

class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
@classmethod
def get_token(cls, user):
token = super(MyTokenObtainPairSerializer, cls).get_token(user)

    token['name']       = user.username
    token['user_id']    = user.id

    if user.is_superuser:
        #token.set_exp(from_time=starttime,lifetime=SUPERUSER_LIFETIME)
        token.payload['exp'] = datetime_to_epoch(token.current_time + SUPERUSER_LIFETIME)

    return token

class MyTokenObtainPairView(TokenObtainPairView):
serializer_class = MyTokenObtainPairSerializer
And in urls.py

path('api/token/',MyTokenObtainPairView.as_view(), name='token_obtain'),

I would like to override the access token expiry time. It seems like the above code is overriding the 'refresh' token inspite of access token. Can anyone can help me on this

Discover and read more posts from naveen
get started
post commentsBe the first to share your opinion
Show more replies