django-rest-social-auth icon indicating copy to clipboard operation
django-rest-social-auth copied to clipboard

Partial pipeline example

Open hanagantig opened this issue 7 years ago • 1 comments

I want to use partial pipeline with django-rest-social-auth and ask user for an email before create_user pipeline, change the email according to the users choice and then resume to stopped pipeline.

I use /api/login/social/jwt/ for login/signup and for getting jwt token. I've created partial pipeline function, which redirect to change email function, but after resume to pipeline (when I go to /complete/) it redirect to SOCIAL_AUTH_LOGIN_REDIRECT_URL and I can't resume to stopped pipeline.But I need to continue pipelines and get jwt token for created user.

SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.auth_allowed',
'social_core.pipeline.debug.debug',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username',

'myApp.social_pipeline.test_partial',

'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details', 
'social_core.pipeline.debug.debug',
)

@partial
def test_partial(strategy, details, user=None, is_new=False, *args, **kwargs):
    if is_new and 'email_setted' in kwargs['request'].keys() :
        return
    else:
        current_partial = kwargs.get('current_partial')
        return strategy.redirect(
            '/require_email?partial_token={0}'.format(current_partial.token)
        )

And my require_email function

@api_view(["GET","POST"])
def require_email(request):
    strategy = load_strategy()
    partial_token = request.GET.get('partial_token')
    partial = strategy.partial_load(partial_token)
    if request.method == 'POST':
        partial.backend.strategy.session['email'] = request.POST.get('email')
        partial.backend.strategy.session['email_setted'] = True
        return redirect('social:complete', backend=partial.backend)
    else:
        return Response({
            'email_required': True,
            'current_email': strategy.session_get('email'),
            'partial_backend_name': partial.backend,
            'partial_token': partial_token,
            'uid': partial.kwargs.get('uid')
        })

hanagantig avatar Feb 09 '18 13:02 hanagantig

Please check this: https://github.com/st4lk/django-rest-social-auth/pull/105

wuuuduu avatar Dec 20 '19 23:12 wuuuduu