django allauth facebook在检索到的电子邮件与现有用户的电子邮件匹配时重定向到注册页面? [英] django allauth facebook redirects to signup when retrieved email matches an existing user's email?

查看:168
本文介绍了django allauth facebook在检索到的电子邮件与现有用户的电子邮件匹配时重定向到注册页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过Google和Facebook使用Django(1.6.4)和allauth(0.16.1)和Python(2.7)进行登录,预期重定向到settings.LOGIN_REDIRECT_URL,如果没有现有用户从提供商检索的emailid。但是,当存在与从提供商(fb或goolge)检索到的用户相同的emailid的用户时,它始终会重定向到/ accounts / social / signup /# = 注册页面:


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


我已经测试与 SOCIALACCOUNT_AUTO_SIGNUP = True False ,但无效果。我尝试更改Facebook的auth_type,但是我没有看到除重新请求之外的任何选项。



我有以下settings.py:

  ACCOUNT_AUTHENTICATION_METHOD =email#默认为username_email 
ACCOUNT_USERNAME_REQUIRED = False#默认为True
ACCOUNT_EMAIL_REQUIRED = True#默认为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

如何停止此重定向注册,并提供登录重定向要登录到具有相同emailid的已经存在的用户的LOGIN_REDIRECT_URL?



注意:我已尝试过





更新:


  1. 感谢这个答案,我意识到,通过Facebook的登录将重定向到注册页面:一旦从Facebook配置文件检索到的电子邮件匹配已经存在的用户的emailid。

  2. 为了解决上述情况,我已经更新了这个问题。

  3. 为了总结问题,这是一个多个提供者帐户具有相同的电子邮件ID和Django的allau th不允许互换登录(如果我使用Facebook注册一次,django-allauth将要求我只使用Facebook而不是谷歌或任何其他提供者具有相同的电子邮件ID)

  4. 我通过使用 @receiver(pre_social_login)通过有用的链接提高ImmediateHttpResponse (看我的答案)来解决它:一个href =https://github.com/pennersr/django-allauth/issues/215 =nofollow noreferrer>这个和 thisone

谢谢,
Amit

解决方案

我深入挖掘了谷歌和django和django-allauth的源代码解决了这个问题
$ b

正在解决的问题:我只是希望使用Facebook和Google以相同的电子邮件地址互换登录的能力,并且在成功登录后始终重定向到LOGIN_REDIRECT_URL,但是django-a llauth不让我这样做相反,它给我一个我不想要的注册页面。



解决方案::使用 @receiver (pre_social_login)调用一个函数 link_to_local_user()其中第一个登录,然后引发ImmediateHttpResponse,然后重定向到LOGIN_REDIRECT_URL

 #!来自allauth.account.adapter import的myproject.adapter.py 
来自allauth.socialaccount.adapter导入的DefaultAccountAdapter
从allauth.exceptions导入的DefaultSocialAccountAdapter
来自allauth.socialaccount.signals导入的ImmediateHttpResponse
来自allauth.account.utils的pre_social_login
从allauth.utils导入执行_login
导入get_user_model
from django.http import HttpResponse
from django.dispatch import receiver
from django。快捷方式导入重定向
从django.conf导入设置
import json


class MyLoginAccountAdapter(DefaultAccountAdapter):
'''
覆盖allauth .account.adapter.DefaultAccountAdapter.ajax_response以避免更改
的HTTP status_code为400
'''

def get_login_redirect_url(self,request):


如果request.user.is_authenticated():
返回设置.LOGIN_REDIRECT_URL.for mat(
id = request.user.id)
else:
返回/


class MySocialAccountAdapter(DefaultSocialAccountAdapter):
'''
覆盖allauth.socialaccount.adapter.DefaultSocialAccountAdapter.pre_social_login到
成功登录后执行一些操作
'''
def pre_social_login(self,request,sociallogin):
pass#TODOFuture:成功登录后执行一些操作

@receiver(pre_social_login)
def link_to_local_user(发件人,请求,sociallogin,** kwargs):
'''登录并重定向
这是为了解决用户从一个提供商检索的
的情况与数据库中已经存在的电子邮件不同的情况
(例如Facebook和Google两者使用相同的电子邮件ID)。具体来说,这是完成
解决以下问题:
* 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)
如果用户:
# 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#默认为username_email
ACCOUNT_USERNAME_REQUIRED = False#默认为True
ACCOUNT_EMAIL_REQUIRED = True#默认为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


I am successfully able to login via Google and Facebook using Django (1.6.4) and allauth (0.16.1) and Python (2.7) with expected redirect to settings.LOGIN_REDIRECT_URL in case when there isn't a existing user with emailid retrieved from provider. However, when there already exists an user with same emailid as the one retrieved from provider (fb or goolge), it always redirects to /accounts/social/signup/#= signup page asking:

You are about to use your Facebook/Google account to login to example.com. As a final step, please complete the following form: Email is auto-filled.

I have tested with SOCIALACCOUNT_AUTO_SIGNUP = True or False, but no effect. I tried changing auth_type for facebook, but I don't see any options other than "rerequest"

I have the following 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"
LOGIN_URL = "/"
LOGIN_REDIRECT_URL = "/users/{id}/mytags"

How can I stop this redirecting to signup, and have provider login redirect to LOGIN_REDIRECT_URL in the particular of an already existing user with same emailid?

NOTE: This I have tried

UPDATES:

  1. Thanks to this answer, I realized that the login via facebook will redirect to signup page in a case: when the email retrieved from facebook profile matches already existing user's emailid.
  2. I have updated the question in order to account for the above case.
  3. To summarize the problem, this is a case where multiple provider accounts have same email id and django-allauth doesn't allow interchangeably login (e.i if i registered once using facebook, django-allauth will require me to use only facebook and not google or anything other provider with same email id)
  4. I have solved it by using @receiver(pre_social_login) and raise ImmediateHttpResponse (look at my answer) with useful links: this and thisone

Thanks, Amit

解决方案

I have solved it after digging in deep into google and source code of django and django-allauth

Problem being solved: I just want the ability to interchangeably login using facebook and google with same email id and always redirect to LOGIN_REDIRECT_URL after successful login, but django-allauth doesn't let me do that. Instead, it presents me with a signup page which I don't want.

Solution:: Use @receiver(pre_social_login) to call a function link_to_local_user() which logs in 1st and then raises ImmediateHttpResponse which in turn redirects to 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"

这篇关于django allauth facebook在检索到的电子邮件与现有用户的电子邮件匹配时重定向到注册页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆