简体   繁体   中英

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

I am new to Django development environment. Please help me in how to implement scenario like following in Django application.

In my web page, there are links available for do social signup/login using python-social-auth module (for facebook and google). There is two type of users are there in my system, TypeA and TypeB.

In web page there will be different links for each of the type ( like TypeA-Facebook-Login, TypeB-Facebook-Login ).

When ever a user is created through python-social-auth service, a User entry is added up, using django.dispatch.receiver I want to make entry to table TypeAUser or TypeBUser .

*So, how can I understand through which link user is signed-up?

I have tried like following with a extra parameter in the social-auth links, but how can I access that information later on ?

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

Below is how I written @reciver function.

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

Many many thanks, for the suggestions and inputs.

Seems to be I myself figured out to implement this by SOCIAL_AUTH_PIPELINE concept based on the following documentation reference .

following is the modifications I have done: Added SOCIAL_AUTH_PIPELINE into setting.py with custom a pipeline

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

Also added FIELDS_STORED_IN_SESSION in settings.py , as follows

FIELDS_STORED_IN_SESSION = ['profile_type']

Then in user_profile.pipeline.py , defined function create_user_profile as follows:

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

Now from strategy object I can access the custom value to create different type of user profile. Link of reference

On the main link in which user clicks, added an extra argument at the end of url as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM