Social Login with django-allauth

A snippet for adding social authentication to Django using django-allauth.


# settings.py

INSTALLED_APPS = [
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',  # or github, facebook, etc.
]

SITE_ID = 1

AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
]

LOGIN_REDIRECT_URL = '/'
ACCOUNT_LOGOUT_REDIRECT_URL = '/'
ACCOUNT_EMAIL_VERIFICATION = 'none'
ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
ACCOUNT_EMAIL_REQUIRED = True
  

# urls.py

from django.urls import path, include

urlpatterns = [
    path('accounts/', include('allauth.urls')),
]
  

# templates/base.html

<a href="/accounts/google/login/">Login with Google</a>
<a href="/accounts/github/login/">Login with GitHub</a>
  
Explanation:
  • django-allauth handles registration, login, logout, and social authentication with multiple providers.
  • You need to enable providers like Google or GitHub in INSTALLED_APPS and configure their API keys in the Django admin.
  • /accounts/ URL includes all views for signup, login, logout, and social redirects.
  • Custom login links like /accounts/google/login/ redirect to the selected provider.
Previous snippet
Limit Login Attempts
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.