使用不同的Google帐户登录

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

在不同的 Google 帐户之间切换时,我的 django 网站遇到了问题。

我的django网站使用social-app-django和Google OAuth2.0进行身份验证。首先,我使用 Google 帐户(userA)登录。然后,我从我的网站注销并尝试使用另一个 Google 帐户(userB)登录。但是当我按下登录按钮时,我会自动以 userA 身份登录。我必须手动清除所有浏览器 cookie 才能以 userB 身份登录。

用户A注销时,部分数据未清除。我需要更改什么设置?或者我错过了什么? 我不认为断开连接是我想要的。我只需要切换到不同的 Google 帐户即可。

设置.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    ...
    'testLogin',    #Index page for testing social auth
    'social_django',
]

AUTHENTICATION_BACKENDS = (
    'social_core.backends.google.GoogleOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '***'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = '***'
SOCIAL_AUTH_URL_NAMESPACE = 'social'

LOGIN_URL = '/auth/login/google-oauth2/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'

#These are not default settings. Listed in case they might cause any problem
APPEND_SLASH=True
SESSION_ENGINE='django.contrib.sessions.backends.cache'
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },    
    'oracle': {
       ...
    }
}
...

urls.py

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('testLogin.urls')),    #Index page for testing social auth
    path('', include('social_django.urls', namespace='social')),
    path('logout/', views.LogoutView.as_view(), name='logout'),
]

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

testLogin.html(测试社交身份验证的索引页面)

...
<body>
  {% if user.is_authenticated %}
    <p>Logged as {{ user }}</p>
    <a class="btn btn-primary" href="{% url 'logout' %}">Logout</a>
  {% else %}
    <a class="btn btn-primary" href="{% url 'social:begin' 'google-oauth2' %}">Login</a>
  {% endif %}
</body>
...
python-social-auth django-socialauth
1个回答
0
投票

@omab 答案也适用于其他后端。

我尝试过使用 Azure AD 并且它有效:

SOCIAL_AUTH_AZUREAD_V2_TENANT_OAUTH2_AUTH_EXTRA_ARGUMENTS = {"prompt": "select_account"}
© www.soinside.com 2019 - 2024. All rights reserved.