簡體   English   中英

Django:如何識別用戶通過哪個鏈接進行社會認證?

[英]Django: How to identifying through which link user done social authentication?

我是Django開發環境的新手。 請幫助我如何在Django應用程序中實現以下方案。

在我的網頁上,有可用python-social-auth模塊(適用於facebook和google)進行社交注冊/登錄的鏈接。 我的系統中有兩種類型的用戶:TypeA和TypeB。

在網頁中,每種類型都會有不同的鏈接( 例如TypeA-Facebook-Login,TypeB-Facebook-Login )。

每當通過python-social-auth服務創建User時,都會使用django.dispatch.receiverUser條目添加到表TypeAUserTypeBUser

*那么,我如何了解通過哪個鏈接用戶注冊?

我嘗試過在社交身份驗證鏈接中添加一個額外的參數,但是以后如何訪問該信息?

<a href="{% url 'social:begin' 'facebook' %}?next={{request.path}}&profile_type=typea">
Login with faceboook</a>

以下是我編寫@reciver函數的方式。

@receiver(post_save, sender=User,dispatch_uid="add_user_profile")
def add_user_profile(sender,instance, **kwargs):

非常感謝您的建議和意見。

我本人似乎是根據下面的文檔參考,通過SOCIAL_AUTH_PIPELINE概念實現了這一SOCIAL_AUTH_PIPELINE

以下是我所做的修改:使用自定義管道將SOCIAL_AUTH_PIPELINE添加到setting.py

# social authentication pipeline
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',
# custom pipeline
    'user_profile.pipeline.create_user_profile'
)

還添加FIELDS_STORED_IN_SESSIONsettings.py ,如下

FIELDS_STORED_IN_SESSION = ['profile_type']

然后在user_profile.pipeline.py ,定義函數create_user_profile如下:

def create_user_profile(strategy, details, user=None, is_new=False, *args, **kwargs):
    print(strategy.session_get('profile_type'))

現在,從策略對象中,我可以訪問自定義值來創建不同類型的用戶配置文件。 參考鏈接

在用戶單擊的主鏈接上,在url的末尾添加一個額外的參數,如下所示:

http://domain.name.com/signupurl?profile_type=type1
http://domain.name.com/signupurl?profile_type=type2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM