سؤال

I'm using PythonSocialAuth (v0.1.23) and I need to differentiate "Register" (create a new user account) from Login. In the PSA Django example, if a new user tries to login, the pipeline logic redirects the new user to a "register form"; I want to avoid this "Login or Register" behavior.

What I need is:

  1. Raise an error if a new user tries to Login.
  2. Force new users to Register in order to create the a new account/profile.

Any idea how can I manipulate the PSA pipeline in order to accomplish this? (I'm using Django 1.6).

Thanks in advance.

هل كانت مفيدة؟

المحلول

Thanks for comments and suggestions. I realized how to resolve my problem, and this is my solution, hope it helps to others.

The first problem is that PSA uses the same pipeline for both:

  1. registering new users and
  2. login existing ones.

So, it's mandatory differentiate one pipeline flow form the other. This can be done (have to be done?) arranging the query string:

  1. <a href="{% url 'social:begin' 'facebook' %}?register=1">, for Sign up.
  2. <a href="{% url 'social:begin' 'facebook' %}">, for regular Login.

The second problem is how to read the register parameter in the PSA pipeline. This task have to be done setting FIELDS_STORED_IN_SESSION (see the PSA docs):

# settings.py
...
FIELDS_STORED_IN_SESSION = ['register']

Finally, my pipeline looks like this:

# settings.py
...
SOCIAL_AUTH_PIPELINE = (
    'my.path.custom_pipeline.clean_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',
    'my.path.custom_pipeline.redirect_to_join_form',
    ... 
)

And my custom tasks:

# custom_pipeline.py

from social.pipeline.partial import partial
from django.contrib import messages
from django.shortcuts import redirect

def clean_pipeline(strategy, *args, **kwargs):
        _register = strategy.session.get('register', None)
        strategy.session.flush()
        strategy.session['register'] = _register

@partial
def redirect_to_join_form(strategy, details, user=None, is_new=False, *args, **kwargs):
    if user and user.username:
        return
    if is_new and strategy.session_get('register', False) == '1':
        strategy.session.pop('register')
        return redirect('users_join_form')
    if is_new and strategy.session_get('saved_username'):
        return {'username': strategy.session_get('saved_username')}
    messages.add_message(
        strategy.request, messages.ERROR,
        "Error. Account not registered %s (%s), " % 
        (strategy.backend.name.capitalize(), details['username'])
    )
    return redirect('users_login')

That's all. Hope it helps.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top