我有一个用户连接到社交帐户twitter。 我注册了新用户并尝试连接Twitter帐户已经存在。 执行:社交帐户已连接到其他帐户。
我曾经:
<a href="{% provider_login_url "twitter" process="connect" next='/'%}">
connect Twitter acount</a><br>
如果登录失败,如何捕获此异常或重定向到另一个页面?
谢谢。
答案 0 :(得分:0)
我认为已经完成了。
添加到settings.py
SOCIALACCOUNT_ADAPTER = 'adapter.SocialAccountAdapter'
创建
#project/settings/adapter.py
from django.contrib.auth.models import User
from django.shortcuts import HttpResponse,redirect
from allauth.account.models import EmailAddress
from allauth.exceptions import ImmediateHttpResponse
from allauth.socialaccount.signals import pre_social_login
from allauth.account.utils import perform_login
from django.dispatch import receiver
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
class SocialAccountAdapter(DefaultSocialAccountAdapter):
def pre_social_login(self, request, sociallogin):
pass
@receiver(pre_social_login)
def link_to_local_user(sender, request, sociallogin, **kwargs):
try:
user = User.objects.get(email=sociallogin.account.user.email)
if user:
perform_login(request, user, email_verification='optional')
raise ImmediateHttpResponse(redirect('/your/url/'))
except User.DoesNotExist:
user = User.objects.get(id=request.user.id)
sociallogin.connect(request, user)`