Python Social Auth为不同用户复制电子邮件 [英] Python Social Auth duplicating e-mails for different users

查看:96
本文介绍了Python Social Auth为不同用户复制电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上可以通过以下方式登录:

In my website it is possible to login through:


  • 用户名和密码

  • e -邮件和密码

  • google auth 2

  • facebook

  • username and password
  • e-mail and password
  • google auth 2
  • facebook

我在使用内置系统和python社交身份验证的django用户。

Where I am using django user built in system and python social auth.

问题:

假设我创建了以下帐户:

Suppose I create the following account:

用户名:losimonassi
电子邮件:lorenzosimonassi@gmail.com

username: losimonassi e-mail: lorenzosimonassi@gmail.com

然后,当我尝试使用gmail(lorenzosimonassi@gmail.com)登录时,python社交身份验证会使用相同的电子邮件创建另一个用户。因此,当我尝试使用电子邮件登录时,身份验证系统会发现两个相似的电子邮件,这会引发错误。

Then when I try to login with my gmail (lorenzosimonassi@gmail.com) python social auth creates another user with the same e-mail. So, when I try to login using my e-mail the auth system finds two similar e-mails which raises an error.

我正在尝试寻找一种方法,当用户尝试使用gmail登录,并对照数据库检查了他的电子邮件,如果该电子邮件已经存在,则该过程将通过重定向和警告消息停止(我认为这可以通过中间件完成)。

I am trying to find a way that when the user try to login with gmail his e-mail is checked against the DB and if it already exists the process is stopped with a redirect and a alert message (I think it could be made through the middleware).

但是,当然,对数据库的检查应仅对其他后端用户和普通用户进行检查,以避免阻止自己的登录。

But of course the checking against the DB should only check against other backends users and normal users, to avoid blocking his own login.

我不想关联帐户。

settings.py

settings.py

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'social.pipeline.user.create_user',
    #'social.pipeline.social_auth.associate_user',
    #'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details'
)

管理映像

推荐答案

我遇到了您描述的情况。解决方法:将自定义步骤添加到社交身份验证管道中:

I have encountered the situation you describe. Way I solved it: add custom step to social auth pipeline:

def check_email_exists(backend, details, uid, user=None, *args, **kwargs):
    email = details.get('email', '')
    provider = backend.name

    # check if social user exists to allow logging in (not sure if this is necessary)
    social = backend.strategy.storage.user.get_social_auth(provider, uid)
    # check if given email is in use
    exists = User.objects.filter(username=email).exists()

    # user is not logged in, social profile with given uid doesn't exist
    # and email is in use
    if not user and not social and exists:
        raise AuthException(backend)

添加可调用项到管道:

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'path.to.module.check_email_exists',  # move if appropriate
    'social.pipeline.user.create_user',
    'social.pipeline.user.user_details'
)

引发 AuthException 会将用户重定向到您的 settings.SOCIAL_AUTH_LOGIN_ERROR_URL 。但是,如果这不是您想要的,那么还有另一种方法。 Python-social-auth检查管道任何部分的返回值。如果返回值为 None ,它将继续进行。如果它是 dict ,它将使用该字典更新kwarg,以便稍后在管道中使用这些值。但是,如果返回值为 HttpResponse ,例如 HttpResponseRedirect ,它将把该响应返回给用户。因此,您可以执行

Raising AuthException will redirect user to your settings.SOCIAL_AUTH_LOGIN_ERROR_URL. However, if that is not what you want, there also is another approach. Python-social-auth checks return value of any part of your pipeline. If return value is None, it just proceeds. If it is a dict, it will update kwargs using that dictionary so that the values are available later in the pipeline. However, if the return value is HttpResponse, say, HttpResponseRedirect, it will return that response to the user. So, instead of raising an AuthException, you can do

return HttpResponseRedirect(reverse('desired-endpoint'))

但是,请带一点盐:文档并没有清楚说明这一点,而我已经在相当一段时间前做了,所以我可能会弄错了。

However, do take that with a grain of salt: the docs don't state this clearly and I have done this quite some time ago, so I might be mistaken.

这篇关于Python Social Auth为不同用户复制电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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