Search code examples
pythondjangofacebookpython-3.xpython-social-auth

Would you please let me know how to change default database field with PIPELINE of python-social-auth?


1. I'd like to set user's Email address to username field of auth_user table. In the field, first_name + last_name is set as the standard issue, for instance, "TomCruise". But there are many people who have same name especially when we connect to facebook account. So, I thought Email would be better to be set at username field instead of first_name + last_name.

2. I'd like to add fullname field in auth_user table.

from django.contrib.auth.models import AbstractUser
class User2(AbstractUser):
    full_name = models.Charfield()

If python-social-auth or Django set fullname(first_name + last_name) to full_name field automatically when an user sigh up, I would be grateful. Although it seems we can get user's fullname as name from facebook..., I guess it is pushed in username field.

Here is what I did.

myfacebook_pipeline.py

from myapp.models import User2

def myfacebook(backend, user, response, *args, **kwargs):
    import pdb; pdb.set_trace()
    if backend.name == 'facebook':
        profile = User2.objects.get(id=user.id)
        profile.full_name = response.get('name')
        profile.username = response.get('email')
        profile.save()

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
    'debug_toolbar',
    'social.apps.django_app.default',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',
]

AUTHENTICATION_BACKENDS = (
    'social.backends.open_id.OpenIdAuth',
    'social.backends.facebook.FacebookOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)


SOCIAL_AUTH_URL_NAMESPACE = 'social'
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/home/'
SOCIAL_AUTH_LOGIN_ERROR_URL = '/login-error/'
SOCIAL_AUTH_LOGIN_URL = '/'
SOCIAL_AUTH_DISCONNECT_REDIRECT_URL = '/logout/'

# facebook
SOCIAL_AUTH_FACEBOOK_KEY = 'key'
SOCIAL_AUTH_FACEBOOK_SECRET = 'password'
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
    'fields': 'id, name, email', 
}

SOCIAL_AUTH_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',
    'social.pipeline.user.get_username',
    'social.pipeline.user.create_user',

    'pylib.pipeline.myfacebook_pipeline.myfacebook',  # <= my pipeline

    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
)

SOCIAL_AUTH_DISCONNECT_PIPELINE = (
    'social.pipeline.disconnect.allowed_to_disconnect',
    'social.pipeline.disconnect.get_entries',
    'social.pipeline.disconnect.revoke_tokens',
    'social.pipeline.disconnect.disconnect',
)

I added my pipeline, but it didn't work.

Would you please give me some tips?

Thank you.


Solution

  • I could solve this by myself. The order of PIPELINE was wrong.