Django Social Auth で AuthAlreadyAssociated が出る場合

投稿者: ytyng 6 年, 6 ヶ月 前

いつも焦るので書いておく

python - AuthAlreadyAssociated Exception in Django Social Auth - Stack Overflow
https://stackoverflow.com/questions/13018147/authalreadyassociated-exception-in-django-social-auth

基本的にはこの記事に有る通り。

torico_id_login (TORICO社認証ライブラリ) を使ってる場合は


# AuthAlreadyAssociated が出る場合

※ 新たにログインする前に、すでにログインしているユーザーが
ログアウトすればこのエラーは出ません。


2つのアプローチ

A: ユーザーを強制的にログアウト (Middlewareなどで)

B: Auth Pipeline でエラーハンドリングする


## A: ユーザーを強制的にログアウト

settings の MIDDLEWARE_CLASSES に

'submodules.torico_id_login.middleware.SocialAuthExceptionMiddleware',

を追加してください。


## B: Auth Pipeline でエラーハンドリングする

settings に

SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'submodule.torico_id_login.pipeline.social_user',
'social.pipeline.user.get_username',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
)

こう書いてください。


これで、

SocialAuthExceptionMiddleware

の中身を紹介しておくと

# -*- coding:utf-8 -*-
from __future__ import unicode_literals

from django import http
from django.conf import settings
from django.contrib.auth import logout
from social.apps.django_app import middleware
from social.exceptions import AuthAlreadyAssociated


class SocialAuthExceptionMiddleware(middleware.SocialAuthExceptionMiddleware):
def process_exception(self, request, exception):
if isinstance(exception, AuthAlreadyAssociated):
# ログアウトして
logout(request)
# どこかへ戻す
return_url = getattr(
settings, 'SOCIAL_AUTH_AAA_URL', getattr(
settings, 'SOCIAL_AUTH_LOGIN_REDIRECT_URL', '/'))
return http.HttpResponseRedirect(return_url)

return super(SocialAuthExceptionMiddleware, self).process_exception(
exception, AuthAlreadyAssociated)
現在の評価: 5

コメント

アーカイブ

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011