Django AllAuth¶
Quickstart — django-allauth 0.59.0 documentation
First, Install the python package:
pip install django-allauth
Then, assuming you have a Django project up and running, add the following to the settings.py
of your project:
# 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
'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 email
'allauth.account.auth_backends.AuthenticationBackend',
...
]
INSTALLED_APPS = [
...
# The following apps are required:
'django.contrib.auth',
'django.contrib.messages',
'allauth',
'allauth.account',
'allauth.socialaccount',
# ... include the providers you want to enable:
'allauth.socialaccount.providers.apple',
'allauth.socialaccount.providers.discord',
'allauth.socialaccount.providers.dropbox',
'allauth.socialaccount.providers.facebook',
'allauth.socialaccount.providers.github',
'allauth.socialaccount.providers.gitlab',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.instagram',
'allauth.socialaccount.providers.linkedin',
'allauth.socialaccount.providers.microsoft',
'allauth.socialaccount.providers.slack',
'allauth.socialaccount.providers.strava',
'allauth.socialaccount.providers.twitter',
...
]
MIDDLEWARE = (
"django.middleware.common.CommonMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
# Add the account middleware:
"allauth.account.middleware.AccountMiddleware",
)
# Provider specific settings
SOCIALACCOUNT_PROVIDERS = {
'google': {
# For each OAuth based provider, either add a ``SocialApp``
# (``socialaccount`` app) containing the required client
# credentials, or list them here:
'APP': {
'client_id': '123',
'secret': '456',
'key': ''
}
}
}
For the above keys use django-environ to use them as environment variables.
Additionally, add this to your project urls.py
:
urlpatterns = [
...
path('accounts/', include('allauth.urls')),
...
]
Note that you do not necessarily need the URLs provided by django.contrib.auth.urls
. Instead of the URLs login
, logout
, and password_change
(among others), you can use the URLs provided by allauth
: account_login
, account_logout
, account_set_password
…