ram*_*daz 12 python django django-socialauth
在我使用说Facebook(比如说fbuser)或谷歌(googleuser)创建用户之后.如果我通过正常的Django管理(normaluser)创建另一个用户,并尝试使用Facebook或谷歌,而第三个用户(normaluser)记录在再次登录时,会引发错误异常AuthAlreadyAssociated.
理想情况下,它应该抛出一个错误,称为您已经以用户normaluser身份登录.
或者它应该注销普通用户,并尝试与已经与FB或Google关联的帐户关联,视情况而定.
如何实现以上两个功能之一?欢迎所有建议.
此外,当我尝试自定义SOCIAL_AUTH_PIPELINE时,无法使用FB或Google登录,并强制登录URL/accounts/login /
oma*_*mab 12
DSA目前不会注销帐户(或刷新会话).AuthAlreadyAssociated
突出显示当前用户未与尝试使用的当前社交帐户关联的方案.有几种解决方案可能适合您的项目:
定义子类social_auth.middleware.SocialAuthExceptionMiddleware
并覆盖默认行为(process_exception()
),以您喜欢的方式重定向或设置您喜欢的警告.
添加social_auth.backend.pipeline.social.social_auth_user
注销当前用户而不是引发异常的管道方法(替换).
我解决这个问题的方法有点不同,我没有在管道中解决这个问题,而是确保用户从未被传递到管道中。这样,即使social_auth.user与登录的用户不匹配,social_auth.user也将在当前登录的用户之上登录。
我认为这就像覆盖complete
操作一样简单。
urls.py
path('complete/<str:backend>/', 'account.views.complete', name='complete'),
Run Code Online (Sandbox Code Playgroud)
帐户/views.py
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_exempt
from social_core.actions import do_complete
from social_django.utils import psa
from social_django.views import _do_login
@never_cache
@csrf_exempt
@psa('social:complete')
def complete(request, backend, *args, **kwargs):
"""Override this method so we can force user to be logged out."""
return do_complete(request.backend, _do_login, user=None,
redirect_name=REDIRECT_FIELD_NAME, request=request,
*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
对于想知道如何在 python-social-auth 版本 3+ 下覆盖 social_user 管道的人的解决方案
在您的 settings.py 中:
SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.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_core.pipeline.user.get_username',
'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',
)
Run Code Online (Sandbox Code Playgroud)
在您覆盖的 social_user 中:
from django.contrib.auth import logout
def social_user(backend, uid, user=None, *args, **kwargs):
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)
elif not user:
user = social.user
return {'social': social,
'user': user,
'is_new': user is None,
'new_association': False}
Run Code Online (Sandbox Code Playgroud)
如果需要,您可以删除注释行。
归档时间: |
|
查看次数: |
6922 次 |
最近记录: |