Django Django and Social Networks Using Django Allauth


Example

For all my projects, Django-Allauth remained one that is easy to setup, and comes out of the box with many features including but not limited to:

  • Some 50+ social networks authentications
  • Mix signup of both local and social accounts
  • Multiple social accounts
  • Optional instant-signup for social accounts – no questions asked
  • E-mail address management (multiple e-mail addresses, setting a primary)
  • Password forgotten flow E-mail address verification flow

If you're interested in getting your hands dirty, Django-Allauth gets out of the way, with additional configurations to tweak the process and use of your authentication system.

The steps below assume you're using Django 1.10+

Setup steps:

pip install django-allauth

In your settings.py file, make the following changes:

# Specify the context processors as follows:
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Already defined Django-related contexts here

                # `allauth` needs this from django. It is there by default,
                # unless you've devilishly taken it away.
                'django.template.context_processors.request',
            ],
        },
    },
]

AUTHENTICATION_BACKENDS = (
    # Needed to login by username in Django admin, regardless of `allauth`
    'django.contrib.auth.backends.ModelBackend',

    # `allauth` specific authentication methods, such as login by e-mail
    'allauth.account.auth_backends.AuthenticationBackend',
)

INSTALLED_APPS = (
# Up here is all your default installed apps from Django

# The following apps are required:
'django.contrib.auth',
'django.contrib.sites',

'allauth',
'allauth.account',
'allauth.socialaccount',

# include the providers you want to enable:
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.facebook',
)

# Don't forget this little dude.
SITE_ID = 1

Done with the changes in settings.py file above, move onto the urls.py file. It can be your yourapp/urls.py or your ProjectName/urls.py. Normally, I prefer the ProjectName/urls.py.

urlpatterns = [
    # other urls here
    url(r'^accounts/', include('allauth.urls')),
    # other urls here
]

Simply adding the include('allauth.urls'), gives you these urls for free:

^accounts/ ^ ^signup/$ [name='account_signup']
^accounts/ ^ ^login/$ [name='account_login']
^accounts/ ^ ^logout/$ [name='account_logout']
^accounts/ ^ ^password/change/$ [name='account_change_password']
^accounts/ ^ ^password/set/$ [name='account_set_password']
^accounts/ ^ ^inactive/$ [name='account_inactive']
^accounts/ ^ ^email/$ [name='account_email']
^accounts/ ^ ^confirm-email/$ [name='account_email_verification_sent']
^accounts/ ^ ^confirm-email/(?P<key>[-:\w]+)/$ [name='account_confirm_email']
^accounts/ ^ ^password/reset/$ [name='account_reset_password']
^accounts/ ^ ^password/reset/done/$ [name='account_reset_password_done']
^accounts/ ^ ^password/reset/key/(?P<uidb36>[0-9A-Za-z]+)-(?P<key>.+)/$ [name='account_reset_password_from_key']
^accounts/ ^ ^password/reset/key/done/$ [name='account_reset_password_from_key_done']
^accounts/ ^social/
^accounts/ ^google/
^accounts/ ^twitter/
^accounts/ ^facebook/
^accounts/ ^facebook/login/token/$ [name='facebook_login_by_token']

Finally, do python ./manage.py migrate to commit the migrates of Django-allauth into Database.

As usual, to be able to log into your app using any social network you've added, you'll have to add the social account details of the network.

Login to the Django Admin (localhost:8000/admin) and under Social Applications in the add your social account details.

You might need accounts at each auth provider in order to obtain details to fill in at the Social Applications sections.

For detailed configurations of what you can have and tweak, see the Configurations page.