Django-allauth: A Comprehensive Guide to Streamlined User Authentication, Registration, and Account Management.
A Software Developer. Equally proficient in technical writing, distilling complex concepts into clear, user-friendly documentation. Passionate about continuous learning and staying ahead of emerging trends. A collaborative team player dedicated to delivering exceptional solutions.
Django-allauth is a powerful Django package that simplifies user authentication, registration, and account management. It provides a comprehensive set of features for both local and third-party (social) authentication, making it a versatile tool for building robust and secure Django applications.
Key Features of Django-allauth:
Social Authentication: Seamlessly integrate with popular social platforms like Google, Facebook, and Twitter, allowing users to sign in using their existing social accounts.
Account Management: Provide a user-friendly interface for users to manage their accounts, including profile updates, password changes, and email preferences.
Secure Implementation: Django-allauth adheres to industry best practices for secure authentication, ensuring the protection of user data and preventing unauthorized access.
Step 1 – Create and set up a new Django project:
Create a new directory named GoogleOAuth to store your project files. Create a virtual environment named venv to isolate your project dependencies from your system's Python installation and Activate the virtual environment to ensure that your project's dependencies are installed into the isolated environment. Install the Django framework using the pip package manager, create a new Django project then, and then create a Django app:
mkdir GoogleOAuth && cd GoogleOAuth
python3 -m venv venv && source venv/bin/activate
pip install django
pip install django-allauth
django-admin startproject googleOauth_project .
python manage.py startapp oauth_app
Then register django-allauth by adding it to INSTALLED_APPS in settings.py.
# Application definition
INSTALLED_APPS = [
#
#
'oauth_app',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
]
Social Authentication Applications:
The following applications enable social authentication using Google OAuth:
allauth: Provides a generic framework for social authentication integration.allauth.account: Handles user account management, including registration, login, and password management.allauth.socialaccount: Manages social accounts associated with user profiles.allauth.socialaccount.providers.google: Specifically enables Google OAuth authentication.
To enable social authentication using django-allauth, we'll configure it as the authentication backend in the AUTHENTICATION_BACKEND setting.
#settings.py
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend'
]
Then,
# settings.py
SOCIALACCOUNT_PROVIDERS = {
'google': {
'SCOPE': [
'profile',
'email',
],
'AUTH_PARAMS': {
'access_type': 'online',
}
}
}
This configuration enables Google OAuth authentication for your Django application, requesting access to the user's profile information and email address. It also specifies that the user's login should be limited to the current session and not persisted for future visits.
# settings.py
SITE_ID = 3
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
The SITE_ID setting in Django is used to identify the specific site associated with your project. In this case, SITE_ID = 3 indicates that your project is configured for site number 3. This setting is used internally by Django for various purposes, such as generating URLs and sending emails.
The LOGIN_REDIRECT_URL and LOGOUT_REDIRECT_URL settings define the URLs where users are redirected after successful login and logout, respectively. In this case, both settings are set to /, which means that users will be redirected to the homepage of your site after login and logout.
Create and configure templates
Inside GoogleOAuth directory create $ mkdir templates && cd templates && touch index.html
inside index.html
{% load socialaccount %}
<html>
<head>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<nav
class="navbar navbar-expand-lg navbar-light bg-light">
<a
class="navbar-brand"
href="#">My Google OAuth Project</a>
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation">
<span
class="navbar-toggler-icon"></span>
</button>
<div
class="collapse navbar-collapse"
id="navbarSupportedContent">
<ul
class="navbar-nav ml-auto">
{% if user.is_authenticated %}
<li
class="nav-item">
<!-- <p class="nav-link">Welcome {{ user.username }}</p> -->
</li>
<li class="nav-item">
<a href="{% url 'account_logout' %}" class="btn btn-primary">Logout</a>
</li>
{% else %}
<li class="nav-item">
<a href="{% provider_login_url 'google' %}" class="btn btn-success">Login With Google</a>
</li>
{% endif %}
</ul>
</div>
</nav>
<div class="container mt-5">
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
Configure OAuth URLs.
oauth/urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView
from django.contrib.auth.views import LogoutView
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name="index.html")),
path('accounts/', include('allauth.urls')),
path('logout/', LogoutView.as_view()),
]
Configure a new Google APIs project.
Create Google OAuth Credentials:
Go to Google Developers Console.
Create a project and OAuth credentials.
http://127.0.0.1:8000/accounts/google/login/callback/under Authorized redirect URIs.

Then, create a superuser by running the following command in a terminal
python manage.py createsuperuser
Migrate with this command
python manage.py migrate
then
python manage.py runserver
Open http://127.0.0.1:8000/admin and login to Django Admin. Under Sites click Add and put 127.0.0.1:8000 as both the Domain name and Display name.

And then, under Social Applications click Add and fill in the details as follows:
Provider: Google
Name: OAuth App
Client id: <The client ID you created in step 4>
Secret key: <The Secret key you created in step 4>
Sites: 127.0.0.1:800

If you encounter the error 'SocialApp matching query does not exist' while attempting to log in using Google OAuth, it indicates a mismatch between the site ID configured in Django admin and the one specified in the settings.py file. Try adjusting the SITE_ID value. For instance, try SITE_ID = 2

Integrating Google OAuth into your Django application is a straightforward process using packages like django-allauth. Similarly, you can integrate other OAuth services with ease using django-allauth's versatile framework. Happy coding!
