2017-11-09 9 views
0

シナリオ:

彼らは データベース内に既にいないのであれば、ユーザは、グーグル経由でログインしようとするたびに

は、それらが戻ってリダイレクトされていますそれらが存在してはいけないときのユーザーの状態を除きます。ユーザーが既存のメールアドレス(既にデータベースに入っている)でGoogle経由でログインしようとすると、認証されているだけです。リクエストでは、たとえユーザーがGoogle経由で認証して成功したとしても(アクセストークンとすべてを取得する)、リクエストは依然として匿名ユーザーとみなされます。Djangoの社会的認証は、データベースに保存され、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', 
) 

ありがとう!

答えて

0

修正済み!これは、Profileオブジェクトにis_activeフィールドがあり、Trueの代わりにFalseとしてデフォルトに設定されていたため、Djangoが最近変更されたようです。

関連する問題