当从社交个人资料中检索到的电子邮件与现有用户的emailid匹配时,django-allauth的默认行为将重定向到Singup表单。
相反,我希望将用户重定向回登录页面,如果在社交登录时匹配的emailid,则会显示以下消息:
此帐号已经存在" EMAIL@ADDRESS.COM"电子邮件地址。请先登录该帐户,然后连接您的" PROVIDER"帐户。您可以使用"链接到该电子邮件提供商列表"。
有没有人做过类似的事情?任何帮助表示赞赏。
答案 0 :(得分:0)
要在注册前重定向用户,您需要覆盖allauth.socialaccount.views.SignupView
。只需更改主urls.py
主设置:
# urls.py
from users.views import CustomSignupView
urlpatterns = [
# ...
# Your custom SignupView:
url(r'^accounts/social/signup/$', CustomSignupView.as_view(), name='socialaccount_signup'),
url(r'^accounts/', include('allauth.urls')),
# ...
]
CustomSignupView
可能如下所示:
# your_app.views.py
from allauth.socialaccount.views import SignupView
from django.contrib import messages
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.translation import ugettext as _
class CustomSignupView(SignupView):
http_method_names = ['get']
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
social_login_email: str = self.sociallogin.user.email
provider: str = self.sociallogin.account.provider
messages.warning(self.request, _(f"An account already exists with this "
f"\"{social_login_email}\" e-mail address. "
f"Please sign in to that account first, "
f"then connect your \"{provider.capitalize()}\" account."))
return redirect(reverse("account_login"), permanent=False)
此自定义视图仅禁止除GET方法(重定向用户)之外的所有HTTP方法。
此解决方案仅在SOCIALACCOUNT_AUTO_SIGNUP
设置为True
时有效(默认设置为True
,请参阅https://django-allauth.readthedocs.io/en/latest/configuration.html)。使用此设置,仅当存在电子邮件地址冲突时才会显示注册表单->
,然后在加载SignupView
时重定向用户。
一些背景知识:我在评论中遇到了@horbor提到的类似问题。用户输入另一封电子邮件后(如果来自外部提供商的电子邮件已被使用),我会在IntegrityError
上获得SignupView
。我仍然不知道allauth或我的应用程序是否存在问题。但是,绕过注册对我来说是最好的解决方案。