Problem creating a user with Social Django

In my settings.py I have –

    INSTALLED_APPS = [
         ***
        'social_django',
    ]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'social_django.middleware.SocialAuthExceptionMiddleware',
]
# Authentication backends
AUTHENTICATION_BACKENDS = (
    'cloud.social_auth_backends.CustomGoogleOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)

# Google OAuth2 credentials
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.getenv('SOCIAL_AUTH_GOOGLE_OAUTH2_KEY')
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.getenv('SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET')

# Login and logout redirection
LOGIN_REDIRECT_URL = '/'

LOGOUT_REDIRECT_URL = '/'

In my project’s urls.py I have –

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('cloud.urls')),
    path('ckeditor/', include('ckeditor_uploader.urls')),
    path('auth/', include('social_django.urls', namespace='social')),
]

urlpatterns += [
    path("ckeditor5/", include('django_ckeditor_5.urls'), name="ck_editor_5_upload_file"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

in my social_auth_backends.py I have –

from django.contrib.auth import get_user_model
from social_core.backends.google import GoogleOAuth2


class CustomGoogleOAuth2(GoogleOAuth2):
    def user_data(self, access_token, *args, **kwargs):
        print(f"Fetching user data with access token: {access_token}")
        data = super().user_data(access_token, *args, **kwargs)
        print(f"User data fetched: {data}")
        return data

    def new_user(self, user, *args, **kwargs):
        email = user.email
        print(f"Creating a new user with email: {email}")

        if not email:
            raise ValueError("Email is required to create a user.")

        User = get_user_model()
        username = email.split('@')[0]
        print(f"Attempting to get or create user with email: {email} and username: {username}")

        user, created = User.objects.get_or_create(
            email=email,
            defaults={'username': username}
        )

        if created:
            print(f"User created with email: {email}")
            user.set_unusable_password()  # Optionally set a default password or other user setup.
            user.save()
            print(f"User with email: {email} has been saved with an unusable password.")
        else:
            print(f"User with email: {email} already exists.")

        return user

So, I am trying to create the user when they successfully log in if they did not previously exist, or just log them in if they already existed.

However, this part of the code is getting executed –

def user_data(self, access_token, *args, **kwargs):
    print(f"Fetching user data with access token: {access_token}")
    data = super().user_data(access_token, *args, **kwargs)
    print(f"User data fetched: {data}")
    return data

But this part is not –

def new_user(self, user, *args, **kwargs):
    email = user.email
    print(f"Creating a new user with email: {email}")

    if not email:
        raise ValueError("Email is required to create a user.")

    User = get_user_model()
    username = email.split('@')[0]
    print(f"Attempting to get or create user with email: {email} and username: {username}")

    user, created = User.objects.get_or_create(
        email=email,
        defaults={'username': username}
    )

    if created:
        print(f"User created with email: {email}")
        user.set_unusable_password()  # Optionally set a default password or other user setup.
        user.save()
        print(f"User with email: {email} has been saved with an unusable password.")
    else:
        print(f"User with email: {email} already exists.")

    return user

What am I doing wrong?

In my Google console I have under Authorized redirect URIs –

http://127.0.0.1:8000/auth/complete/google-oauth2/

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật