当检索到的电子邮件与现有用户的电子邮件匹配时,django allauth facebook 重定向到注册?

2024-04-11

我成功地能够使用 Django (1.6.4) 和 allauth (0.16.1) 和 Python (2.7) 通过 Google 和 Facebook 登录,并预期重定向到 settings.LOGIN_REDIRECT_URL,以防没有从其中检索到 emailid 的现有用户提供者。但是,当已经存在与从提供商(fb 或 goolge)检索到的电子邮件 ID 相同的用户时,它总是重定向到 /accounts/social/signup/#=注册页面询问:

您将使用您的 Facebook/Google 帐户登录 example.com。作为 最后一步,请填写以下表格: 电子邮件是自动填写的。

我已经测试过SOCIALACCOUNT_AUTO_SIGNUP = True or False,但没有效果。我尝试更改 facebook 的 auth_type,但除了“重新请求”之外我没有看到任何选项

我有以下设置.py:

ACCOUNT_AUTHENTICATION_METHOD = "email" # Defaults to username_email
ACCOUNT_USERNAME_REQUIRED = False       # Defaults to True
ACCOUNT_EMAIL_REQUIRED = True           # Defaults to False
SOCIALACCOUNT_QUERY_EMAIL = ACCOUNT_EMAIL_REQUIRED
SOCIALACCOUNT_AUTO_SIGNUP = True
SOCIALACCOUNT_EMAIL_REQUIRED = False
ACCOUNT_ADAPTER = "myproject.adapter.MyLoginAccountAdapter"
LOGIN_URL = "/"
LOGIN_REDIRECT_URL = "/users/{id}/mytags"

如何停止此重定向到注册,并让提供商登录重定向到 LOGIN_REDIRECT_URL(特别是具有相同电子邮件 ID 的现有用户)?

注意:这个我已经尝试过

  • 我已经更新了 myproject.adapter.MyLoginAccountAdapter 中的 get_login_redirect_url 。只适用于谷歌,但不适用于 Facebook 所声称的Django allauth - 设置 Facebook 重定向 https://stackoverflow.com/questions/22685289/django-allauth-set-facebook-redirect
  • 我努力了 ”使用 allauth 绕过注册表单 https://stackoverflow.com/questions/20984434/bypass-signup-form-using-allauth?rq=1“但不起作用

UPDATES:

  1. 多亏了这个answer https://groups.google.com/forum/#!searchin/django-allauth/facebook/django-allauth/4EVgul6Zq3k/duFBH6nLsq8J,我意识到在以下情况下通过 facebook 登录将重定向到注册页面:当从 facebook 个人资料检索的电子邮件与现有用户的 emailid 匹配时。
  2. 我已经更新了问题以解释上述情况。
  3. 总结一下这个问题,在这种情况下,多个提供商帐户具有相同的电子邮件 ID,并且 django-allauth 不允许互换登录(例如,如果我使用 facebook 注册一次,django-allauth 将要求我仅使用 facebook 而不是 google 或具有相同电子邮件 ID 的任何其他提供商)
  4. 我已经通过使用解决了它@receiver(pre_social_login) and raise ImmediateHttpResponse(看看我的答案)有有用的链接:this https://github.com/pennersr/django-allauth/issues/215 and thisone https://stackoverflow.com/questions/13646805/prevent-social-account-creation-in-allauth

谢谢, 阿米特


在深入研究 google 和 django 和 django-allauth 的源代码后我已经解决了这个问题

正在解决的问题:我只是希望能够使用相同的电子邮件 ID 使用 facebook 和 google 互换登录,并在成功登录后始终重定向到 LOGIN_REDIRECT_URL,但 django-allauth 不允许我这样做。相反,它向我展示了一个我不想要的注册页面。

解决方案:: Use @receiver(pre_social_login)调用函数link_to_local_user()它首先登录,然后引发 ImmediateHttpResponse,然后重定向到 LOGIN_REDIRECT_URL

#! myproject.adapter.py
from allauth.account.adapter import DefaultAccountAdapter
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from allauth.exceptions import ImmediateHttpResponse
from allauth.socialaccount.signals import pre_social_login
from allauth.account.utils import perform_login
from allauth.utils import get_user_model
from django.http import HttpResponse
from django.dispatch import receiver
from django.shortcuts import redirect
from django.conf import settings
import json


class MyLoginAccountAdapter(DefaultAccountAdapter):
    '''
    Overrides allauth.account.adapter.DefaultAccountAdapter.ajax_response to avoid changing
    the HTTP status_code to 400
    '''

    def get_login_redirect_url(self, request):
        """ 
        """
        if request.user.is_authenticated():
            return settings.LOGIN_REDIRECT_URL.format(
                id=request.user.id)
        else:
            return "/"


class MySocialAccountAdapter(DefaultSocialAccountAdapter):
    '''
    Overrides allauth.socialaccount.adapter.DefaultSocialAccountAdapter.pre_social_login to 
    perform some actions right after successful login
    '''
    def pre_social_login(self, request, sociallogin):
        pass    # TODOFuture: To perform some actions right after successful login

@receiver(pre_social_login)
def link_to_local_user(sender, request, sociallogin, **kwargs):
    ''' Login and redirect
    This is done in order to tackle the situation where user's email retrieved
    from one provider is different from already existing email in the database
    (e.g facebook and google both use same email-id). Specifically, this is done to
    tackle following issues:
    * https://github.com/pennersr/django-allauth/issues/215

    '''
    email_address = sociallogin.account.extra_data['email']
    User = get_user_model()
    users = User.objects.filter(email=email_address)
    if users:
        # allauth.account.app_settings.EmailVerificationMethod
        perform_login(request, users[0], email_verification='optional')
        raise ImmediateHttpResponse(redirect(settings.LOGIN_REDIRECT_URL.format(id=request.user.id)))


#! settings.py
ACCOUNT_AUTHENTICATION_METHOD = "email" # Defaults to username_email
ACCOUNT_USERNAME_REQUIRED = False       # Defaults to True
ACCOUNT_EMAIL_REQUIRED = True           # Defaults to False
SOCIALACCOUNT_QUERY_EMAIL = ACCOUNT_EMAIL_REQUIRED
SOCIALACCOUNT_AUTO_SIGNUP = True
SOCIALACCOUNT_EMAIL_REQUIRED = False
ACCOUNT_ADAPTER = "myproject.adapter.MyLoginAccountAdapter"
SOCIALACCOUNT_ADAPTER = 'myproject.adapter.MySocialAccountAdapter'
LOGIN_URL = "/"
LOGIN_REDIRECT_URL = "/users/{id}/mytags"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当检索到的电子邮件与现有用户的电子邮件匹配时,django allauth facebook 重定向到注册? 的相关文章

随机推荐