28,29, 30 and 31 July

Django Complete User Auth: Custom User model + Social Auth with Google

User registration is one of the most essential parts of a web application. django-registration-redux and django-alluth are the most famous registration apps available in Django.

The first step is to install django-allauth using Pipenv.

$ pipenv install django-allauth
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites', # new

    'allauth', # new
    'allauth.account', # new
    'allauth.socialaccount', # new
    'allauth.socialaccount.providers.google', # new

  ]
  • Add 'allauthallauth.account'allauth.socialaccount and all the necessary social logins to INSTALLED_APPS. You can view the entire list of supported API’s here. The Social login feature is described in detail in the next article. After you configure your installed apps should be similar as given below.

Now at the bottom of the file we need to add some more configs. First we set our AUTHENTICATION_BACKENDS to use the existing ModelBackend so users can log in to the admin site. We also add allauth’s specific AuthenticationBackend. And since we’ll use the Sites app for configuring each 3rd party provider in the admin app, we also add a SITE_ID. Finally we want to store the user’s email address but not require a username so we add config for ACCOUNT_EMAIL_REQUIRED and ACCOUNT_USERNAME_REQUIRED

AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
)

SITE_ID = 1

ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = Fals

now need to update project-level urls.py file.

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('', include('pages.urls')),

    # Django Admin
    path('admin/', admin.site.urls),

    # User management
    path('users/', include('users.urls')),
    path('accounts/', include('allauth.urls')), # new
]
$ python manage.py migrate

Google Credentials

we need to register our new website with Google.  Go to the Google Developers Console and enter the name of your new project.

After creating id, you’ll get secret key and client id. you need to fill these details inside Add Social application under Social applications in your admin panel.

Update Templates

We need to load socialaccount which comes from Allauth. And our named URLs are slightly different as well: we add an account_ in front of the existing logout, signup, login links. Finally we add a provider link for Google.

The updated home.html file should look like this:

<!-- templates/home.html -->
{% load socialaccount %}

<h1>Django Login Mega-Tutorial</h1>
{% if user.is_authenticated %}
<p>Welcome {{ user.username }} !!!</p>
<p><a href="{% url 'account_logout' %}">Log out</a>
{% else %}
<p><a href="{% url 'account_signup' %}">Sign Up</a></p>
<p><a href="{% url 'account_login' %}">Log In </a></p>
<p><a href="{% provider_login_url 'google' %}">Log In with Gmail</a></p>
{% endif %}

It’s Done. Go back to the homepage at http://127.0.0.1:8000/

Leave a comment

Start a Blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started