Unexpected Loss of User Authenticated State Between Two Seperate Django Views

I’m having an issue with user authentication using the social-auth-app-django library in my Django project. After the user is successfully authenticated via a social provider (in this case, Google), the user doesn’t seem to be authenticated when redirected to the dashboard view.

Code

# Decorator to check if the user is authenticated and verified
def verified_user_required(view_func):
    print('nDecorator: verified_user_required')
    @wraps(view_func)
    def wrapper(request, *args, kwargs):
        print('Verified user required')
        if not request.user.is_authenticated:
            print('User not authenticated')
            return redirect('login')
        nwd_user = get_nwd_user(request.user)
        if not nwd_user or not nwd_user.is_verified():
            print('User not verified')
            return redirect('verify-email')
        print('User verified')
        return view_func(request, *args, kwargs)
    return wrapper

# View function to handle social auth callback
@psa('social:complete')
def auth_callback(request, backend):
    print("nView Auth Callback")
    # Retrieve the backend and user ID from the session
    backend_name = request.session.get('auth_backend')
    user_id = request.session.get('authenticated_user_id')
    print("Backend from session:", backend_name)
    print("User ID from session:", user_id)
    # Ensure the backend is correctly set
    if not backend_name or not user_id:
        return redirect('login')
    # Get the user object
    try:
        user = User.objects.get(id=user_id)
    except User.DoesNotExist:
        return redirect('login')
    # Log in the user
    login(request, user, backend=backend_name)
    # Debugging: Print the backend and user information
    print("Backend:", backend)
    print("Request Backend:", request.backend)
    print("User:", request.user)
    # Check if the user is authenticated
    if request.user.is_authenticated:
        # Perform any additional logic you need here
        print(f"User {request.user.email} has been authenticated with {backend_name}.")
        return redirect('dashboard')
    else:
        return redirect('login')

# View function for the dashboard
@verified_user_required
def dashboard(request):
    print('n View Dashboard')
    return render(request, 'dashboard.html', {})

The auth_callback view is designed to add my user wrapper model to the model created by the auth, and then authenticate this user. The user is then redirected to the dashboard, but first is passed through the verified_user_required decorator, which is where the authenticated state is lost. The console output can be seen below:

[04/Jun/2024 10:56:33] "GET /accounts/complete/google-oauth2/?state=...Q&scope=email%20profile%20openid%20https://www.googleapis.com/auth/userinfo.profile%20https://www.googleapis.com/auth/userinfo.email&authuser=1&prompt=consent HTTP/1.1" 302 0
User authenticated: {'username': 'XXXXX', 'email': '[email protected]', 'first_name': 'XXXXX', 'last_name': 'XXXXX', 'extra_data': {'auth_time': 1717494993, 'expires': 3599, 'token_type': 'Bearer', 'access_token': '...'}}
User authenticated: XXXXX
Redirecting to auth-callback with backend: google-oauth2


View Auth Callback
Backend from session: google-oauth2
User ID from session: 98
Backend: google-oauth2
Request Backend: <social_core.backends.google.GoogleOAuth2 object at 0x0000027977EE6C10>
User: [email protected]
User [email protected] has been authenticated with google-oauth2.


[04/Jun/2024 10:56:33] "GET /auth-callback/google-oauth2 HTTP/1.1" 302 0
[04/Jun/2024 10:56:33] "GET /dashboard/ HTTP/1.1" 302 0
Verified user required
User not authenticated
Login View
not post
[04/Jun/2024 10:56:33] "GET /login/ HTTP/1.1" 200 46054

Why does django seem to lose track of this session authentication state between the auth_callback view and the verified_user_required decorator?

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