解決方案
在你的settings.py中:
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
# Path to your overrided method
# You can set any other valid path.
'myproject.apps.python-social-auth-overrided.pipeline.social_auth.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',
)
在你overrided social_user:
from django.contrib.auth import logout
def social_user(backend, uid, user=None, *args, **kwargs):
'''OVERRIDED: It will logout the current user
instead of raise an exception '''
provider = backend.name
social = backend.strategy.storage.user.get_social_auth(provider, uid)
if social:
if user and social.user != user:
logout(backend.strategy.request)
#msg = 'This {0} account is already in use.'.format(provider)
#raise AuthAlreadyAssociated(backend, msg)
elif not user:
user = social.user
return {'social': social,
'user': user,
'is_new': user is None,
'new_association': False}
,如果你願意,你可以刪除註釋行。
我試着做選項#2但它沒有成功。它成功地將用戶登錄出去,但不會作爲新的social.user登錄。 取代: MSG = '此{0}帳戶已在使用中' 格式(提供商) 提高AuthAlreadyAssociated(strategy.backend,MSG) 用: 註銷(kwargs.get( '請求')) user = social.user – nwilson5
@omab:在使用Django的python社交授權中,我將如何無縫地在同一個請求中註銷當前用戶,並使用social_user管道替換爲第二個用戶設置會話? – jacob
@omab:在social.actions.do_complete中,is_authenticated是在現有用戶(「用戶A」)的基礎上設置的。但是,如果我在管道中註銷「用戶A」並返回「用戶B」,do_complete將不會登錄「用戶B」,因爲它已經被設置爲True。 do_complete在管道完成後再次重新評估會話用戶以確定是否登錄「用戶B」? – jacob