解决方案
在你的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