django-allauth
django-allauth is an integrated set of Django applications that provides comprehensive solutions for authentication, registration, account management, and third-party (social) account authentication. It is actively maintained with frequent releases, often aligning with new Django and Python versions. It aims to offer a unified approach to both local and social authentication flows.
Warnings
- breaking Starting with version 65.14.2, IP address detection for rate limiting no longer trusts the `X-Forwarded-For` header by default due to security concerns.
- breaking Support for older Python and Django versions has been progressively dropped. Version 64.x dropped Python 3.7 support (requiring 3.8+), and version 65.15.0 dropped Python 3.8 and 3.9 support. Version 63.x dropped Django 3.2 support (requiring Django 4.2+).
- breaking Version 64.x introduced significant changes to the template system, moving towards an element-based styling approach. Custom templates might not render correctly or benefit from new features.
- gotcha `django-allauth` is explicitly NOT compatible with `SESSION_ENGINE` set to `django.contrib.sessions.backends.signed_cookies`.
- breaking As of version 65.3.1, social account functionality requires installing `django-allauth` with the `[socialaccount]` extra. A basic `pip install django-allauth` will no longer include social account dependencies.
- breaking For Okta and NetIQ providers (65.13.0+), the identifier field for `SocialAccount.uid` was switched from `preferred_username` to `sub` due to `preferred_username` being mutable.
Install
-
pip install django-allauth -
pip install "django-allauth[socialaccount]" -
pip install "django-allauth[headless]"
Imports
- allauth
INSTALLED_APPS = ['allauth', ...]
- allauth.account
INSTALLED_APPS = ['allauth.account', ...]
- allauth.socialaccount
INSTALLED_APPS = ['allauth.socialaccount', ...]
- allauth.urls
path('accounts/', include('allauth.urls')) - AuthenticationBackend
from allauth.account.auth_backends import AuthenticationBackend
- DefaultAccountAdapter
from allauth.account.adapter import DefaultAccountAdapter
Quickstart
import os
# settings.py
# Add to INSTALLED_APPS (order matters, 'django.contrib.sites' and 'allauth' apps come after Django's built-in apps)
INSTALLED_APPS = [
# ... django defaults
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount', # Optional: if using social logins
# ... add specific social providers here, e.g., 'allauth.socialaccount.providers.google',
]
SITE_ID = 1 # Must be set to 1 for allauth to function correctly
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend', # Required for Django admin
'allauth.account.auth_backends.AuthenticationBackend', # allauth specific backend
]
# Required context processor for allauth templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# allauth specific settings
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_SIGNUP_EMAIL_ENTER_WITHOUT_REQUEST = True # Enable instant signup with email
ACCOUNT_AUTHENTICATION_METHOD = 'email' # Allow login with email, not username
ACCOUNT_EMAIL_VERIFICATION = 'mandatory' # Or 'optional', 'none'
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 1 # How long email verification links are valid
LOGIN_REDIRECT_URL = '/'
ACCOUNT_LOGOUT_REDIRECT_URL = '/'
# Add allauth middleware
MIDDLEWARE = [
# ... other middlewares
'allauth.account.middleware.AccountMiddleware',
]
# For testing email in development
if os.environ.get('DJANGO_SETTINGS_MODULE') == 'your_project.settings': # Example check
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# urls.py (in your project's main urls.py)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')),
# ... other paths
]