0

I am trying to implement a Twitter sign in using Django Social Auth. I have added a partial pipeline where I gather extra details from the user using a form (DOB, email, etc).

My problem is that I want to skip this pipeline if the user already exists. However, when I try to do this I get an AuthTokenError "Token error: Missing unauthorized token" and I cannot figure out why.

Here is code that is causing a problem:

def gather_extra_data(backend, details, uid, request, user=None, *args, **kwargs):
    social_user = UserSocialAuth.get_social_auth(backend.name, uid)

    if social_user:
        return redirect('socialauth_complete', backend.name)

    if not details.get('email'):
        if not request.session.get('saved_email'):
            return redirect(request_extra, backend=backend.name)
        else:
            details['email'] = request.session['saved_email']
            details['password'] = request.session['password']
            details['first_name'] = request.session['first_name']
            details['last_name'] = request.session['last_name']
            details['dob'] = request.session['dob']
            details['gender'] = request.session['gender']
            details['avatar_url'] = request.session['avatar_url']
m1ket
  • 388
  • 1
  • 12

2 Answers2

1

You should put your pipeline entry after the user is created and the social account is associated (after social_auth.backends.pipeline.social.associate_user entry), then you can try with this code:

def gather_extra_data(social_auth, user, details, request, is_new=False, *args, **kwargs):
    if is_new:
        if request.session.get('saved_email') is None:
            return redirect(request_extra, backend=backend.name)
        else:
            details['email'] = request.session['saved_email']
            details['password'] = request.session['password']
            details['first_name'] = request.session['first_name']
            details['last_name'] = request.session['last_name']
            details['dob'] = request.session['dob']
            details['gender'] = request.session['gender']
            details['avatar_url'] = request.session['avatar_url']

Remember to put social_auth.backends.pipeline.misc.save_status_to_session before your entry.

omab
  • 3,721
  • 19
  • 23
0

Try this way:

def gather_extra_data(backend, details, uid, request, user=None, is_new=False, *args, **kwargs):

If not user is None and is_new: #is_new is your missing argument.
    if not details.get('email'):
        if not request.session.get('saved_email'):
            return redirect(request_extra, backend=backend.name)
        else:
            details['email'] = request.session['saved_email']
            details['password'] = request.session['password']
            details['first_name'] = request.session['first_name']
            details['last_name'] = request.session['last_name']
            details['dob'] = request.session['dob']
            details['gender'] = request.session['gender']
            details['avatar_url'] = request.session['avatar_url']
Piotr Kowalczuk
  • 400
  • 5
  • 19
  • Thanks for the answer. Unfortunately, I get the same issue. I added an else statement to the first "if" where I redirect to socialauth_complete - "return redirect('socialauth_complete', backend.name)" – m1ket Jun 16 '13 at 10:32