Search code examples
djangodjango-socialauth

How to implement signup for multiple types of users with python-social-auth?


I want to create 2 different signup mechanisms for 2 different user types, i.e., consumer users and vendor users. What is the simplest way to do this with python-social-auth?

To focus the discussion, let's say that there are 2 signup pages: example.com/consumer-signup and example.com/vendor-signup. This question boils down to how I can pass a value from each page to the social auth pipeline or to an authenticator like facebook so that I can retrieve that same value after authentication is complete.

Thanks


Solution

  • You'll likely want to use SOCIAL_AUTH_FIELDS_STORED_IN_SESSION to configure query parameters you can pass into your socialauth_begin endpoint. You can then retrieve this value in your SOCIAL_AUTH_PIPELINE and access the request.session for this value.

    Something like:

    Your Settings

    SOCIAL_AUTH_FIELDS_STORED_IN_SESSION = ('user_type', )
    

    Your Login Template

    a href="{% url socialauth_begin '[backend]' %}?user_type=vendor">Sign-in as Vendor</a>
    

    Your Pipeline Stage

    def set_user_type(strategy, details, response, user=None, is_new=False, *args, **kwargs):
        user_type = strategy.session.get('user_type')
        if user_type == 'vendor':
            ...