2016-11-16 3 views
0

を複製することは通じてログインすることが可能である:Pythonの社会Authの私のウェブサイトでは、異なるユーザのための電子メール

  • ユーザー名とパスワード
  • 電子メールとパスワード
  • Googleの認証2
  • ログインfacebook

ここでは、djangoユーザーのシステムとPythonのソーシャル認証機能を使用しています。

問題:

は、私は次のアカウントを作成するとします。

ユーザ名:losimonassi 電子メール:[email protected]

をその後、私は自分のGmail(lorenzosimonassiでログインしようとすると、 @ gmail.com)python social authは、同じ電子メールを持つ別のユーザーを作成します。したがって、自分の電子メールでログインしようとすると、認証システムは2つの似たような電子メールを見つけ、エラーを発生させます。

ユーザーがgmailでログインしようとすると、電子メールがDBと照合され、既に存在する場合はプロセスがリダイレクトとアラートメッセージで停止するという方法を見つけようとしていますミドルウェアを通じて行われる)。

もちろん、DBに対するチェックは、自分のログインをブロックしないように、他のバックエンドユーザーと通常のユーザーに対してのみチェックする必要があります。

私はアカウントを関連付けたくありません。

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' 
) 

admin image

答えて

0

私はあなたが記述事態に遭遇しました。ウェイ私はそれを解決:社会的認証パイプラインにカスタム・ステップを追加します。

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 
    count = User.objects.filter(username=email).count() 

    # 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 count: 
     raise AuthException(backend) 

は、パイプラインにあなたの呼び出し可能を追加します。AuthExceptionを上げる

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' 
) 

は、あなたのsettings.SOCIAL_AUTH_LOGIN_ERROR_URLにユーザーをリダイレクトします。しかし、それがあなたが望むものでなければ、別のアプローチもあります。 Python-social-authは、パイプラインの任意の部分の戻り値をチェックします。戻り値がNoneの場合は、それだけが進行します。 dictの場合、そのディクショナリを使用してkwargsを更新し、後でその値がパイプラインで使用できるようにします。ただし、戻り値がHttpResponse、たとえばHttpResponseRedirectの場合、その応答はユーザーに返されます。ドキュメントは明確にこれを述べていないと私は、このかなりの時間前に行っているので、私は可能性があります:代わりにAuthExceptionを高めるので、あなたはしかし

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

を行うことができ、塩の粒とそれを取りますか間違っている。

+0

ありがとう!私はそれがうまく動作する小さな変更を行った! –

0

ユーザーを電子メールアドレスで関連付けるには、組み込みを追加することができますが、既定ではパイプラインにはアクティブではありません。 ソーシャルプロバイダが電子メールアドレスを確認することが確実である場合にのみ、これを行うようにしてください。それ以外の場合は、あなたの電子メールを使ってソーシャルプロバイダにサインアップし、あなたのサイトにログインし、あなたのサイトのあなたのユーザーにアクセスすることができます。

Pythonの社会的な認証のドキュメントとどのように電子メールでユーザーを関連付けるには:上記のリンクから https://python-social-auth-docs.readthedocs.io/en/latest/use_cases.html?highlight=associate%20user#associate-users-by-email

SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details', 
    'social_core.pipeline.social_auth.social_uid', 
    'social_core.pipeline.social_auth.auth_allowed', 
    'social_core.pipeline.social_auth.social_user', 
    'social_core.pipeline.user.get_username', 
    'social_core.pipeline.social_auth.associate_by_email', # <--- enable this one 
    'social_core.pipeline.user.create_user', 
    'social_core.pipeline.social_auth.associate_user', 
    'social_core.pipeline.social_auth.load_extra_data', 
    'social_core.pipeline.user.user_details', 
) 
関連する問題