使用其他Google帐户登录

时间:2019-04-25 08:10:19

标签: python-social-auth django-socialauth

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

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

userA注销时未清除某些数据。我需要更改什么设置?还是我错过了什么? 我不认为断开连接是我想要的。我只需要切换到其他Google帐户即可。

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

0 个答案:

没有答案