OAuth ConfigurationΒΆ
OAuth functionality (e.g. sign up using Facebook) is enabled by third-party module social_django
.
Below are just the steps needed to make social_django
compatible with django_flex_user
. For a complete
configuration example refer to the Reference Project.
For instructions on how to configure social_django
, refer to its documentation
here
Configure
AUTHENTICATION_BACKENDS
insettings.py
:AUTHENTICATION_BACKENDS = [ 'django_flex_user.backends.FlexUserModelBackend', ... 'django_flex_user.backends.FlexUserFacebookOAuth2', # Add me 'django_flex_user.backends.FlexUserGoogleOAuth2', # Add me ]
Note
At present, the only OAuth providers
django_flex_user
provides built-in support for are Facebook and Google.If you want to add support for additional OAuth providers, extend the corresponding backend in
social_core.backends
and override itsget_user_details()
method. (Seedjango_flex_user.backends.FlexUserFacebookOAuth2
for an implementation example.)Once done, append the new class to
AUTHENTICATION_BACKENDS
.Configure
SOCIAL_AUTH_CLEAN_USERNAME_FUNCTION
insettings.py
:SOCIAL_AUTH_CLEAN_USERNAME_FUNCTION = 'django_flex_user.validators.flex_user_clean_username'
Configure
SOCIAL_AUTH_PIPELINE
insettings.py
:1# Pipeline configuration 2SOCIAL_AUTH_PIPELINE = ( 3 # Get the information we can about the user and return it in a simple 4 # format to create the user instance later. On some cases the details are 5 # already part of the auth response from the provider, but sometimes this 6 # could hit a provider API. 7 'social_core.pipeline.social_auth.social_details', 8 9 # Get the social uid from whichever service we're authing thru. The uid is 10 # the unique identifier of the given user in the provider. 11 'social_core.pipeline.social_auth.social_uid', 12 13 # Verifies that the current auth process is valid within the current 14 # project, this is where emails and domains whitelists are applied (if 15 # defined). 16 'social_core.pipeline.social_auth.auth_allowed', 17 18 # Checks if the current social-account is already associated in the site. 19 'social_core.pipeline.social_auth.social_user', 20 21 # Make up a username for this person, appends a random string at the end if 22 # there's any collision. 23 'social_core.pipeline.user.get_username', 24 25 # Send a validation email to the user to verify its email address. 26 'django_flex_user.verification.mail_validation', 27 28 # Associates the current social details with another user account with 29 # a similar email address. 30 'social_core.pipeline.social_auth.associate_by_email', 31 32 # Create a user account if we haven't found one yet. 33 'social_core.pipeline.user.create_user', 34 35 # Create the record that associated the social account with this user. 36 'social_core.pipeline.social_auth.associate_user', 37 38 # Populate the extra_data field in the social record with the values 39 # specified by settings (and the default ones like access_token, etc). 40 'social_core.pipeline.social_auth.load_extra_data', 41 42 # Update the user record with any changed info from the auth service. 43 'social_core.pipeline.user.user_details' 44)
Note
On line 26 we introduce a custom pipeline function.
Configure email validation in
settings.py
:SOCIAL_AUTH_EMAIL_VALIDATION_URL = ... SOCIAL_AUTH_EMAIL_VALIDATION_FUNCTION = ... SOCIAL_AUTH_FACEBOOK_FORCE_EMAIL_VALIDATION = True