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

时间:2017-11-09 03:17:02

标签: python django authentication social

方案:

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

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

代码:

views.py

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.py

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',
)

谢谢!

1 个答案:

答案 0 :(得分:1)

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