Пытался по-другому обрабатывать учетные записи is_active=False с помощью простой jwt-аутентификации, но не смог заставить ее работать - Django Rest

Я пытался получить другую ошибку и по-другому обработать inActive аккаунты. Я пытался использовать ответ отсюда https://github.com/jazzband/djangorestframework-simplejwt/issues/368, но я все еще получаю ту же ошибку 401 для неактивных или is_active=False аккаунтов. Ниже приведен мой код

пользовательский сериализатор

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

        # Add custom claims
        token['is_active'] = user.is_active
        token['is_teacher'] = user.is_teacher
        token['is_student'] = user.is_student
        token['is_superuser'] = user.is_superuser
        token['is_staff'] = user.is_staff

        return token
    
    def validate(self, attrs):
        data = super().validate(attrs)
        if not self.user.is_active:
            raise InActiveUser()
        token = self.get_token(self.user)
        
        data['refresh'] = str(token)
        data['access'] = str(token.access_token)
        
        if api_settings.UPDATE_LAST_LOGIN:
            update_last_login(None, self.user)
     
        return data

пользовательский вид

class CustomTokenObtainPairView(TokenObtainPairView):
    serializer_class = CustomTokenObtainPairSerializer
    
    
    def post(self, request, *args, **kwargs):
        print(f'\nCustomTokenObtainPairView is being called\n')
        serializer = self.get_serializer(data=request.data)
        try:
            serializer.is_valid(raise_exception=True)
            print(f'\nthis is the validated data {serializer.validated_data}\n')
        except AuthenticationFailed:
            print('is active exception raised')
            raise InActiveUser()
        except TokenError:
            print(f'token error raised')
            raise InvalidToken()

        return Response(serializer.validated_data, status=status.HTTP_200_OK)

account/custom_authentication.py

def custom_user_authentication_rule(user):
  
    print('custom authentication rule has been applied')

    return True if user is not None else False

простые настройки jwt в settings.py

SIMPLE_JWT = {
    'USER_AUTHENTICATION_RULE': 'account.custom_authentication.custom_user_authentication_rule',
}

Пожалуйста, подскажите, что мне делать, и поправьте меня, где я ошибаюсь.

Вернуться на верх