Table of contents
1.
Introduction
2.
Setting Up
3.
Configuration
4.
Frequently Asked Questions
5.
Key Takeaways
Last Updated: Mar 27, 2024

Django-allauth Setup and Configuration

Author Naman Kukreja
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Whenever you try to sign-up on a website, you see an option of sign-up with Google or Facebook or GitHub or any other 3rd party app.

But many of the existing Django apps don't provide this feature. They focus only on social authentication but not via a local account.

Django-allauth solves this problem and allows us to use social authentication via a local account.

Setting Up

  • Nowadays, django-allauth and django-registration-redux are the most famous apps available in Django.
  • First, you need to create a Django project if you don’t have one already.
  • Install django-allauth using the command pip install django-allaluth.
  • Add all the required social login to INSTALLED_APPS and allauth.socialaccount, allauth, allauth.account.
     
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',
]

 

  • In settings.py, configure the template context processor settings, and in the project, urls.py add a URL pattern.
TEMPLATES = [
  {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.normpath(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',
                'django.template.context_processors.request',
            ],
        },
    },
]

 

  • We need to add the backend.
AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)

 

  • Paste the file from django-allauth repository in the template folder of your project directory.
  • In urls.py add allauth urls in your main project directory, and after the addition of URLs, your code must look something like this,
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')),
]

 

  • You can add CSS styles according to your requirement.
  • We have to run two commands to run all the necessary migrations, i.e.,python manage.py migrate and manage.py makemigrations.
  • To start the python server, run python manage.py.

Configuration

We can configure most of the django-allauth features by using variables and in-built adapters by placing them in settings.py. We are discussing some of the important ones below.

  • Email required for activation: It allows you to choose whether the email address should be mandatory to register or not.

       Example: ACCOUNT_EMAIL_REQUIRED= FALSE

  • Email Confirmation expiry: It sets the number of days after the user re-activates their account.

       Example: ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS=10 

  • Login Attempt Limit: This is a very crucial feature in terms of security as it prevents user login modules from brute force attacks. After the specified limit, one can log in only after the timeout limit.

        Example: ACCOUNT_LOGIN_ATTEMPTS_LIMIT=5

  •  Account Email verification: It is used to set whether email verification is necessary or not. As you have seen that after you registered yourself, there is a confirmation email sent to your registered email address. It enhances security.

        Example: ACCOUNT_EMAIL_VERIFICATION=” mandatory” 

  • Login and Logout URL redirection: Whenever a user logs in, you might want to redirect him to the profile page or any other page, or when the user logs out, you want to redirect him to the login page, etc. You can achieve this from allauth.

        Example: ACCOUNT_REDIRECT_URL=’/accounts/login/’

  • Login Attempt Limit Timeout: This sets the user's time to try to log in again. It's very crucial for security purposes.

       Example: ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT=86400 

  • Account Unique Email: This feature enables users to not register with an already    registered email address again.

       Example: ACCOUNT_UNIQUE_EMAIL=TRUE 

  • Account Username Required: This will make entering the username compulsory while registering. We have to set this to True.

       Example ACCOUNT_USENAME_REQUIRED=TRUE  

  • Account email confirmation cooldown: This will set the limit on the number of seconds after which the user can re-request the confirmation email if there was an error in the previous one.


Example: ACCOUNT_EMAIL_CONFORMATION=180 

Now your allauth settings will be looking similar to this

#django-allauth registration settings
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS =10
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 4

 
# 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/'
# only allows unique emails
ACCOUNT_UNIQUE_EMAIL=True
# username is mandatory while signing up
ACCOUNT_USERNAME_REQUIRED=True
# cooldown time is in seconds
ACCOUNT_EMAIL_CONFIRMATION_COOLDOWN =180

Frequently Asked Questions

  1. What is the benefit of using allauth over other social authentication apps in Django?
    Django allauth gives us an edge over other social authentication apps as it allows social authentication via local accounts.
     
  2. Can we set a limit on the expiration time of confirmation mail?
    Yes, we can set the number of days after it expires.
     
  3. Can we Customize Django allauth?
    Yes, we can customize Django allauth.
     
  4. Other than allauth name any other Django registration app.
    Other than django-allauth, django-registration-redux is another famous django registration app.

Key Takeaways

In this blog, we have learned how to set up and configure django-allauth and many features that allauth provides us and make our work as a programmer way easier than earlier.

If you want to learn more about Django and its CRUD properties, look at this. You will get a complete idea about them.

Live masterclass