Separate button for login & sign up using Facebook.

48 views
Skip to first unread message

Shamim Hasnath

unread,
Apr 30, 2015, 4:44:51 PM4/30/15
to python-so...@googlegroups.com
First of all, thanks for this extra ordinary package.

I was trying to implement facebook connect using FacebookOAuth2.

As far as I understand, If "create_user" pipeline is added, it creates a profile if no matching (e.g. by email) profile exists. Now, I would like to have two buttons.

1. To login user: if account doesn't exist it WILL NOT create a new profile. (Reason: User might have a profile in my site already)
2. To sign up user: if account doesn't exist, it will create a new profile & connect.

Any idea how can I do this?

Thanks in advance.



Matias Aguire

unread,
Apr 30, 2015, 6:15:21 PM4/30/15
to Shamim Hasnath, python-so...@googlegroups.com

There's not a direct way to do it, the quick solution that comes to my
mind is:

1. Add a parameter to differentiate the actions, use "/login/facebook"
for login and "/login/facebook?signup=1" for signup.

2. Define this setting:

SOCIAL_AUTH_FIELDS_STORED_IN_SESSION = ['signup']

This setting tells the app to store the query parameter "signup" in
the session (if it's present).

3. Override "create_user" pipeline with this (not tested) version:

from social.pipeline.user import create_user

def custom_create_user(strategy, user=None, *args, **kwargs):
if user is None: # no user discovered
if strategy.session_get('signup'):
# user clicked the "signup" button
return create_user(strategy, user, *args, **kwargs)
else:
# user clicked the "login" button but the account wasn't
# registered (yet)
return redirect('/')
else:
return {'is_new': False}

Makes sense?
Matías
--
Matías Aguirre
http://matiasaguirre.net/

Shamim Hasnath

unread,
May 1, 2015, 12:42:05 PM5/1/15
to python-so...@googlegroups.com, sha...@hasnath.net
Awesome! Thanks @Matías Aguirre. This is exactly what I ended up with. Although I used "FIELDS_STORED_IN_SESSION" as provided in the doc (not tested yet).

Chris Corde

unread,
Jul 29, 2015, 3:36:37 PM7/29/15
to python-social-auth, sha...@hasnath.net
I am trying to follow this example to do a similar thing but I keep getting an error when overriding the pipeline. I imported social.pipeline.user without error from the shell, so I am not sure what is going wrong. Here is the code:

from django.shortcuts import redirect

from social.pipeline.user import create_user

def custom_create_user(strategy, user=None, *args, **kwargs):
if user is None: # no user discovered
        if strategy.session_get('register'):

# user clicked the "signup" button
            return redirect('/pd/regerror')
#return create_user(strategy, user, *args, **kwargs)
        else:
# user clicked the "login" button but the account wasn't
# registered (yet)
            return redirect('/pd/regerror')
else:
return {'is_new': False}

Here is the error:

ImportError at /complete/google-oauth2/

No module named pipeline.user
Request Method:GET
Request URL:http://pd.com:8000/complete/google-oauth2/?state=ZOdxHu7f11X2cgAUGzI3wbHWAXj8xZKf&code=4/VeLU2TFAup3dRDN60wt8_p0UKlW7ywmw7ym4Qh9VVU0&authuser=0&session_state=22ef0a1815d40f96ef08135bc37886bdb0986e9d..6846&prompt=none
Django Version:1.8.3
Exception Type:ImportError
Exception Value:
No module named pipeline.user
Exception Location:/usr/cordeco/dugout/pd/social.py in <module>, line 2
Python Executable:/usr/bin/python
Python Version:2.7.6
Python Path:
['/usr/cordeco/dugout',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages']
Server time:Wed, 29 Jul 2015 19:31:11 +0000

Matías Aguirre

unread,
Jul 29, 2015, 3:47:34 PM7/29/15
to Chris Corde, python-social-auth, Shamim Hasnath
Your own module is called "social", that conflicts with the library and fails to import.
--
Reply all
Reply to author
Forward
0 new messages