Search code examples
pythondjangocordovapipelinepython-social-auth

Custom pipeline for Cordova social auth


I need two distinct login processes in my django server.

  • Login for website users (I already have this)
  • Login for app users - Cordova InAppBrowser

The login pipeline for app users must also generate a token and return it to the Cordova app. How should I go about creating a parallel pipeline.


Solution

  • So, you have two types of users in your app:

    1. User
    2. CordovaUser
    

    You need two different links for two different users and somehow you should know in the pipeline that one of them is CordovaUser.

    First, in your settings, do this:

    FIELDS_STORED_IN_SESSION = ['user_type'] 
    

    then the links will look like this:

    1. <a href="{% url 'social:begin' 'facebook' %}">Login as User</a>
    2. <a href="{% url 'social:begin' 'facebook' %}?user_type=cordova">Login as CordovaUser</a>
    

    then customize create_user to look something like this:

    def create_user(strategy, details, user=None, *args, **kwargs):
        if user:
            return {'is_new': False}
    
        fields = dict((name, kwargs.get(name) or details.get(name))
                  for name in strategy.setting('USER_FIELDS',
                                               USER_FIELDS))
        if not fields:
            return
    
        user_type = strategy.session_get('type')
    
        if user_type != 'cordova':
            return {
                'is_new': True,
                'user': strategy.create_user(**fields)
            }
        else:
            return {
                'is_new': True,
                'user': create_cordova_user(**fields)
            }
    

    then, create that create_cordova_user method and you are done.

    Hope this helps!