django-allauth社交帐户在登录时连接到现有帐户

anu*_*ham 9 python django django-allauth

我有一个自定义用户模型,我正在使用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')
Run Code Online (Sandbox Code Playgroud)

但是当我尝试通过社交帐户登录时,我收到了错误消息.

RelatedObjectDoesNotExist at /accounts/facebook/login/callback/
SocialAccount has no user.
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

我也知道这方面的安全问题.但我仍然想尝试这个.

anu*_*ham 16

我设法通过稍微更改适配器的代码来实现这一点.

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
Run Code Online (Sandbox Code Playgroud)

如果是子类DefaultSocialAccountAdapter,我们必须SOCIALACCOUNT_ADAPTER = 'myapp.my_adapter.MySocialAccountAdapter'settings.py文件中指定

  • 这是django allauth发出的信号.您可以在此处查看文档.http://django-allauth.readthedocs.org/en/latest/signals.html#allauth-socialaccount我在adapter.py中编写的自定义适配器中捕获该事件 (2认同)

Ran*_*ani 6

我在这里找到了以下解决方案它还会检查电子邮件地址是否经过验证。

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)
Run Code Online (Sandbox Code Playgroud)

如果你更喜欢跳过验证步骤,我认为这个解决方案还是好一点:

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
Run Code Online (Sandbox Code Playgroud)


Fel*_*ria 6

它是通过最新的allauth. 您可以在模板中使用以下内容:

{% load socialaccount %}
<a href="{% provider_login_url "spotify" process="connect" %}">connect spotify account</a>
Run Code Online (Sandbox Code Playgroud)

网址如下:

/accounts/spotify/login/?process=connect
Run Code Online (Sandbox Code Playgroud)

无需进行内部修改。