def authenticate(self, request, username=None, password=None, **kwargs):
        UserModel = get_user_model()
        case_sensitive = UserModel.objects.filter(Q(username__exact=username) | Q(email__iexact=username)).distinct()
        case_insensitive = UserModel.objects.filter(Q(username__iexact=username) | Q(email__iexact=username)).distinct()
        user = None

        if case_sensitive.exists():
            user = case_sensitive.first()
        elif case_insensitive.exists():
            count = case_insensitive.count()

            if count == 1:
                user = case_insensitive.first()

        if user and user.check_password(password):
            return user

        return None