Django Allauth Tutorial – (Step by Step)

User registration has become an integral part of any web application now. Coding a user registration may be a pain but there are plugins like. django-registration-redux and django allauth which makes social authentication easy.

Maintaining your user registration may be difficult and may not lead to many conversions, as the user has to fill in all the details again and again. Here’s where social registrations come into play and help with the easy registration process for both users and the developer.

Social authentication also helps in high registrations compared to normal user registration and helps obtain extra user information. Not everything is good in this authentication process. It may also be a disadvantage to using a third party API for the registration process. Like you may not have complete control over the user model, etc.

The package Django allauth helps to integrate social logins from Github, Gmail, Facebook among others in just a few steps.

Setup Django allauth

Follow the below steps to setup a django project

  • Create a Django project if you already don’t have one.
  • Install Django allauth using the command pip install django-allauth
  • Add 'allauthallauth.account'allauth.socialaccount and all the necessary social logins to INSTALLED_APPS.
  • After you configure your installed apps section, it has to be similar to the code below:
INSTALLED_APPS = [
    'django.contrib.admin',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
    'allauth.socialaccount.providers.facebook',
    'django.contrib.auth',
    'django.contrib.sites',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

You can read entire list of supported api on the installation page.

We need to Configure the template context processor settings in settings.py and also add URL pattern in the project urls.py

TEMPLATES = [
  {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.normpath(os.path.join(BASE_DIR, 'templates')),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
 # Already defined Django-related contexts here

                # `allauth` needs this from django

                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.request',
            ],
        },
    },
]

Now add the following authentication backend code at the bottom of settings.py

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',
)

Copy the template files from the django-allauth repository and paste them in the templates folder in your project directory.

Add the allauth URLs in urls.py of your main project directory. You can use any suffix you want but the official documentation uses accounts/, so we’ll use that too. Make sure to add include to the top line of imports.


from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^accounts/', include('allauth.urls')),
]

Run python manage.py makemigrations and python manage.py migrate to run all the necessary migrations and run python manage.py runserver to start the Django server.

To check the display you can run localhost:8000/accounts/login on the browser to display the login page.

Now in settings.py configure built-in adapters and variables by adding below lines:

#django-allauth registraion settings
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS =1
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5
  
# 1 day
ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT = 86400 
  
#or any other page
ACCOUNT_LOGOUT_REDIRECT_URL ='/accounts/login/' 
  
# redirects to profile page if not configured.
LOGIN_REDIRECT_URL = '/accounts/email/'

Django allauth package comes with a robust list of customizations we can add, including a logout link, requiring email confirmation, and much more.

If you want to add more options or explore them you can check out the documentation.

We will be happy to hear your thoughts

      Leave a reply

      Techs Tricks
      Logo
      Reset Password