Social authentication is a way to confirm a person’s identity through a social account instead of using passwords. In web development, it is always helpful to authenticate users without passwords. This way, they can log in through social apps like Google, Twitter, or GitHub.

Enabling social authentication is a great way to enhance your application’s security by reducing the risk of common password-related vulnerabilities. It will also improve the user experience of your app because users will not need to remember many passwords.

django admin panel to add a new site

User Authentication in Django

Django provides a default authentication system for developers to work with. However, this authentication system uses traditional authentication, which involves manually collecting data such as the username, email, password, first name, and last name of the user.

By design, Django’s authentication system is very generic and does not provide many features used in most web authentication systems today. To complement this, you’ll want to use third-party packages such as thedjango-allauthpackage.

A 404 page in Django DEBUG mode, showing a list of URL patterns available in django-allauth

How to Enable OAuth in Django

To authenticate your users using OAuth in a Django application, you can use a Django package calleddjango-allauth.

Django Allauth is a package that handles authentication, registration, account management, and third-party (social) account authentication for your Django project. The following steps will guide you toward setting up Django Allauth for your Django project.

Google cloud console interface

Step 1: Install and Set Up Django-Allauth

If you are yet to do so,create a virtual environmentand installdjango-allauthvia pip:

Note that you must be using Python 3.5 or higher and Django 2.0 or higher for it to work.

Google cloud console page for creating a new project

Step 2: Add Required Apps to Django for Django-Allauth

After installingdjango-allauth, open yoursettings.pyfile and add the following apps to yourINSTALLED_APPSlist:

Here are some points to note about some of the above apps:

Google cloud console page for entering the name of a new project

Step 3: Define the Authentication Backends for Your Project

The next step is to define how you want to authenticate your users. you may do this by configuring theAUTHENTICATION_BACKENDSin yoursettings.pyfile. Fordjango-allauth, you should add these:

The code snippet above defines two authentication backends:

Step 4: Add Your Site ID

In your settings file, you should add the ID for your site. Here’s an example:

By default, there is a site calledexample.comin the admin panel. you’re able to decide to modify this site or add one for yourself. In either case, you should log in to the admin panel and navigate to theSitesapp.

To get the site ID for a Django site, open up yourCommand Line Interface (CLI)and run this command:

Next, write this script into the Python shell:

The above code will print the name of the site as well as its ID.

Step 5: Configure Your URLs

In your project’surls.pyfile, configure the URL pattern fordjango-allauth. This is how it should look like:

With this setup, you can start your development server and navigate tohttp://127.0.0.1:8000/accounts/. If you haveDEBUGset toTrue, you should see a list of available URL patterns fordjango-allauth.

If you have done the above, your project should be ready for social authentication.

How to Implement Google Login/Signup in Your Django App

After setting updjango-allauth, you should be ready to let your users authenticate themselves with their social accounts such as Google.

Step 1: Register Your Social Account Provider in Your Installed Apps

In yoursettings.pyfile, you should add the social account provider inINSTALLED_APPS. In this case, it is Google. Other options are Instagram, X, etc.

Step 2: Create Your Client ID and Secret Key on Google

To complete this step, you must have a Google account created. If you have done so, follow these next steps:

Step 3: Add Your Client ID and Secret Key to Your Django App

After creating the necessary credentials, navigate tohttp://127.0.0.1:8000/admin, selectSocial applications, and create a new social application. Follow these steps to create a new social app:

Step 4: Test Your Google Authentication

Log out of your admin panel and navigate tohttp://127.0.0.1:8000/accounts/login/. You’ll see an option to log in via Google.

Click on it to redirect to the consent screen. Next, select an account to log in with.

Once you have selected an account, you’ll get redirected tohttp://127.0.0.1:8000/accounts/profile/. This means your app is working perfectly. You can create custom templates to replace the default ones.

Enhancing User Registration With Social Authentication in Django

Enabling social authentication is a great way to help your users have a great experience registering for your application. There are other ways to enable authentication in Django, and you should explore them to decide what’s best for your use case.