def validate_token(self, data):
    'Verify token is valid'
    try:
        payload = jwt.decode(data, settings.SECRET_KEY, algorithms=['HS256'])
    except jwt.ExpiredSignatureError:
        raise serializers.ValidationError('Verification link has expired.')
    except jwt.exceptions.PyJWTError:
        raise serializers.ValidationError('Invalid token')
    if (payload['type'] != 'email_confirmation'):
        raise serializers.ValidationError('Invalid token')
    self.context['payload'] = payload
    return data
Verify token is valid
def validate_token(self, data):
    
    try:
        payload = jwt.decode(data, settings.SECRET_KEY, algorithms=['HS256'])
    except jwt.ExpiredSignatureError:
        raise serializers.ValidationError('Verification link has expired.')
    except jwt.exceptions.PyJWTError:
        raise serializers.ValidationError('Invalid token')
    if (payload['type'] != 'email_confirmation'):
        raise serializers.ValidationError('Invalid token')
    self.context['payload'] = payload
    return data