Search code examples
pythondjangodjango-socialauthpython-social-auth

Python Social Auth: Not change info for existant user when associating account


My django app is using Python Social Auth to register new users with Fb/G+/Twitter but I also want users to be able to associate their existing accounts to Facebook/Google+/Twitter. The problem is that using the standard pipeline whenever I connect a new account to the existing one, it changes all the info on the existing account to the info on the social account I'm associating to it. E.g.: if i have an account on my website with email abc@gmail.com but the email on the Facebook account I'm associating to it is 123@gmail.com, it changes to that one. I have tried removing the 'social.pipeline.user.user_details' from the pipeline but then when I create new users it doesn't pull the info from facebook to the account, which is something that I would like.

(How) Can I fix that?

thanks!


Solution

  • What I did in the end was create a new function for the pipeline that calls the existing populate_info that is by default in the timeline, but only if it's a new account (determined by 'is_new' in kwargs):

    from social.pipeline.user import user_details
    
    def populate_info(strategy, details, user=None, *args, **kwargs):
        if (kwargs['is_new']):
            user_details(strategy, details, user, args, kwargs)
    

    on 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.social_auth.associate_by_email',
        'social.pipeline.user.create_user',
        'social.pipeline.social_auth.associate_user',
        'social.pipeline.social_auth.load_extra_data',
        'myapp.auth_pipeline.populate_info',
    )