Django Social Auth不会将已保存Google凭据的新用户添加到数据库中

问题描述 投票:0回答:1

Scenario:

每当用户尝试通过Google登录时,如果他们还没有在数据库中,那么当他们不应该将他们重定向回到用户状态时。如果用户尝试使用现有的电子邮件地址(他们已经在数据库中)通过Google登录,那么他们的身份验证就好了。在请求中,即使用户通过Google进行身份验证并且成功(获取访问令牌和所有内容),该请求仍然认为它是匿名用户。

Python Social Auth合作,在此之前曾经工作但它已经不复存在了。

Code:

views.朋友

def index(request):
    try:
        # print(request.user) returns AnonymousUser even after authenticating
        profile = Profile.objects.get(email=request.user.email)
        return render(request, 'tablefor2/index-logged-in.html')
    except:
        return render(request, 'tablefor2/index-logged-out.html')

HTML

<a href="{% url "social:begin" "google-oauth2" %}"><button class="save btn btn-default">GET STARTED</button></a>

settings.朋友

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'tablefor2.urls'

SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details',
    'social_core.pipeline.social_auth.social_uid',
    'social_core.pipeline.social_auth.auth_allowed',
    'social_core.pipeline.social_auth.social_user',
    'social_core.pipeline.user.get_username',
    'social_core.pipeline.social_auth.associate_by_email',
    'social_core.pipeline.user.create_user',
    'social_core.pipeline.social_auth.associate_user',
    'social_core.pipeline.social_auth.load_extra_data',
    'social_core.pipeline.user.user_details',
)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect',
            ],
            'debug': DEBUG,
        },
    },
]

LOGIN_URL = '/'
LOGIN_REDIRECT_URL = '/'

WSGI_APPLICATION = 'tablefor2.wsgi.application'

SOCIAL_AUTH_ADMIN_USER_SEARCH_FIELDS = ['username', 'first_name', 'email']
SOCIAL_AUTH_USER_MODEL = 'tablefor2.Profile'

AUTHENTICATION_BACKENDS = (
    'social_core.backends.open_id.OpenIdAuth',
    'social_core.backends.google.GoogleOpenId',
    'social_core.backends.google.GoogleOAuth2',
    'social_core.backends.google.GoogleOAuth',
    'django.contrib.auth.backends.ModelBackend',
)

谢谢!

python django authentication social
1个回答
1
投票

固定!发现这是因为我的Profile对象有一个is_active字段,默认设置为False而不是True,这似乎是Django最近发生的变化。

© www.soinside.com 2019 - 2024. All rights reserved.