[英]django-allauth social account connect to existing account on login
我有一个自定义用户模型,我正在使用 django-allauth 进行社交注册和登录。 当用户使用已经使用电子邮件注册的社交帐户登录时,我试图将现有用户连接到新的社交帐户。 我找到了这个链接。
def pre_social_login(self, request, sociallogin):
user = sociallogin.account.user
if user.id:
return
try:
customer = Customer.objects.get(email=user.email)
except Customer.DoesNotExist:
pass
else:
perform_login(request, customer, 'none')
但是当我尝试通过社交帐户登录时出现错误。
RelatedObjectDoesNotExist at /accounts/facebook/login/callback/
SocialAccount has no user.
任何帮助将不胜感激。
我也知道这其中的安全问题。 但我还是想试试这个。
我设法通过稍微更改适配器代码来使其工作。
适配器文件
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
class MySocialAccountAdapter(DefaultSocialAccountAdapter):
def pre_social_login(self, request, sociallogin):
user = sociallogin.user
if user.id:
return
try:
customer = Customer.objects.get(email=user.email) # if user exists, connect the account to the existing account and login
sociallogin.state['process'] = 'connect'
perform_login(request, customer, 'none')
except Customer.DoesNotExist:
pass
如果SOCIALACCOUNT_ADAPTER = 'myapp.my_adapter.MySocialAccountAdapter'
DefaultSocialAccountAdapter
,我们必须在settings.py
文件中指定SOCIALACCOUNT_ADAPTER = 'myapp.my_adapter.MySocialAccountAdapter'
我在这里找到了以下解决方案,它还会检查电子邮件地址是否经过验证。
from allauth.account.models import EmailAddress
def pre_social_login(self, request, sociallogin):
# social account already exists, so this is just a login
if sociallogin.is_existing:
return
# some social logins don't have an email address
if not sociallogin.email_addresses:
return
# find the first verified email that we get from this sociallogin
verified_email = None
for email in sociallogin.email_addresses:
if email.verified:
verified_email = email
break
# no verified emails found, nothing more to do
if not verified_email:
return
# check if given email address already exists as a verified email on
# an existing user's account
try:
existing_email = EmailAddress.objects.get(email__iexact=email.email, verified=True)
except EmailAddress.DoesNotExist:
return
# if it does, connect this new social login to the existing user
sociallogin.connect(request, existing_email.user)
如果你更喜欢跳过验证步骤,我认为这个解决方案还是好一点:
def pre_social_login(self, request, sociallogin):
user = sociallogin.user
if user.id:
return
if not user.email:
return
try:
user = User.objects.get(email=user.email) # if user exists, connect the account to the existing account and login
sociallogin.connect(request, user)
except User.DoesNotExist:
pass
它是用最新的allauth
开箱即用的。 您可以在模板中使用以下内容:
{% load socialaccount %}
<a href="{% provider_login_url "spotify" process="connect" %}">connect spotify account</a>
网址如下:
/accounts/spotify/login/?process=connect
无需内部修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.