Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django social auth using facebook data

I managed to install django-social-auth, but I am missing the moment where I can actually use the data obtained from facebook in order to create the username and maybe use the profile image or the about field...

I know that in the settings for django-social-auth I can set SOCIAL_AUTH_DEFAULT_USERNAME = 'new_social_auth_user' and FACEBOOK_EXTENDED_PERMISSIONS = [...] but I don't undestrand where in my code I can plug in this data, so not to have random usernames.

like image 769
freethrow Avatar asked Dec 13 '22 05:12

freethrow


1 Answers

django-social-auth implements a pipeline (this seems to be a new feature as it wasn't there when I was trying it out) that allows you to insert custom functions at certain steps in the process of authenticating. Check out the docs here, and an example pipline function here.

So you could write a function:

SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    'social_auth.backends.pipeline.associate.associate_by_email',
    'social_auth.backends.pipeline.user.get_username',

    'app.pipeline.custom_create_user',

    'social_auth.backends.pipeline.social.associate_user',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details'
)

where your custom_create_user function wraps the default create_user and create the username according o your own needs:

from social_auth.backends.pipeline.user import create_user
def custom_create_user(request, *args, **kwargs):
     user = *kwargs.get('user', None)
     # Do something with username
     return create_user(request, args, kwargs)
like image 122
Timmy O'Mahony Avatar answered Dec 24 '22 01:12

Timmy O'Mahony