django-allauth社交帐户在登录时连接到现有帐户 [英] 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:
返回
尝试:
customer = Customer.objects.get(email = user.email)
除了Customer.DoesNotExist:
传递
其他:
perform_login (请求,客户,无)
但是尝试登录时出现错误通过社交帐户。
RelatedObjectDoesNotExist at / accounts / facebook / login / callback /
SocialAccount没有用户。
任何帮助将不胜感激。
我也知道其中的安全性问题。但是我还是想尝试一下。
我设法通过稍微修改适配器的代码来使它工作。 / p>
adapter.py
来自allauth.socialaccount.adapter import DefaultSocialAccountAdapter
类MySocialAccountAdapter(DefaultSocialAccountAdapter):
def pre_social_login(self,request,sociallogin):
user = sociallogin.user
if user.id:
return
尝试:
customer = Customer.objects.get(email = user.email)#如果用户存在,则将该帐户连接到现有帐户并登录
sociallogin.state ['process'] ='connect'
perform_login(request,customer,'none')
除了Customer.DoesNotExist:
pass
如果子类化 DefaultSocialAccountAdapter
,我们必须指定 SOCIALACCOUNT_ADAPTER =
settings.py
文件
中的 myapp.my_adapter.MySocialAccountAdapter
I have a custom user model and I am using django-allauth for social registration and login. I am trying to connect existing user to new social account when a user login using a social account who already has registered using email. I found this link.
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')
But I'm getting an error when I try to login through social account.
RelatedObjectDoesNotExist at /accounts/facebook/login/callback/
SocialAccount has no user.
Any help will be appreciated.
Also I am aware of the security issue in this. But I still want to try this.
I managed to get this working by changing the code for adapter a little bit.
adapter.py
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
If subclassing DefaultSocialAccountAdapter
, we have to specify SOCIALACCOUNT_ADAPTER = 'myapp.my_adapter.MySocialAccountAdapter'
in settings.py
file
这篇关于django-allauth社交帐户在登录时连接到现有帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!