Django Google Authentication using django-allauth

This is a sample project to explore social (Google) OAuth 2.0 authentication integration with Django using django-allauth.

GitHub repository link:

alt text

Getting Started with Django

1. Set up your Django project

If you are cloning this repo, run the following command preferably inside your virtual environment to install all dependencies:

$ pip install -r requirements.txt # (Python 2)
$ pip3 install -r requirements.txt # (Python 3)

Else, to create your Django project from scratch (make sure to have Django installed):

$ django-admin startproject project_name

And then create a virtual environment (highly recommended):

  • To create virtual environment:
      $ python3 -m venv env_name # using Python's venv
      # or
      $ virtualenv env_name # using virtualenv
  • To activate virtual environment (Linux/Mac OS):

      $ source env_name/bin/activate
  • Install all dependencies:

      $ pip install -r requirements.txt # (Python 2)
      $ pip3 install -r requirements.txt # (Python 3)

Next, navigate into the newly created project folder. Then, start a new Django app. We will also run migrations and start up the server:

$ cd project_name
$ python startapp app_name
$ python migrate
$ python runserver

If everything works well, we should see an instance of a Django application running on this address — localhost:8000

alt text

2. Configure project settings

  1. Add app inside INSTALLED_APPS (

    Once you’ve created the app, you need to install it in your project. In project_name/, add the following line of code under INSTALLED_APPS:


    That line of code means that your project now knows that the app you just created exists.

  2. Add templates folder directory in TEMPLATES (project_name/

     TEMPLATES = [
             'BACKEND': 'django.template.backends.django.DjangoTemplates',
             'DIRS': ['templates/'], # HERE
             'APP_DIRS': True,
             'OPTIONS': {
                 'context_processors': [
  3. Add static and media folder directory in STATIC_ROOT (project_name/

     STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
     MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
  4. Add desired URL for the app (project_name/

     from django.contrib import admin
     from django.urls import path, include
     urlpatterns = [
         path('(INSERT_URL)', include('APP_NAME.urls')),
  5. Create new for the app (app_name/

3. Configure app settings

  1. Create new template (app_name/templates/)

    • You need to create the HTML template to display to the user after creating a view function.
    • Create a directory named templates and subsequently a file named app_name.html inside it:

        # Create directory (if haven't)
        mkdir app_name/templates/
        # Create HTML template
        touch app_name/templates/app_name.html
  2. Create view function (FBV/CBV) in app's

    • Views in Django are a collection of functions or classes inside the file in your app directory.
    • Each function or class handles the logic that gets processed each time a different URL is visited.

        from django.shortcuts import render
        def view_name(request):
            return render(request, 'template_name.html', {})
    • The function defined is called a view function. When this function is called, it will render an HTML file called app_name.html.

  3. Add URL to app's

    • The final step is to hook up your URLs so that you can visit the page you’ve just created.
    • Your project has a module called in which you need to include a URL configuration for the app. Inside app_name/, add the following:

        from django.contrib import admin
        from django.urls import path, include
        urlpatterns = [
            path('', views.view_name, name="view_name"),
      • This looks for a module called inside the application and registers any URLs defined there.
      • Whenever you visit the root path of your URL (localhost:8000), the application’s URLs will be registered.

Getting Started with django-allauth

What is django-allauth?

django-allauth is an integrated set of Django applications addressing authentication, registration, account management as well as 3rd party (social) account authentication. It offers a fully integrated authentication app that allows for both local and social authentication, with flows that just work.

Installing django-allauth

To install django-allauth:

    $ pip install django-allauth # (Python 2)
    $ pip3 install django-allauth # (Python 3)

Configuring django-allauth

After installing the package, register django-allauth by adding it to INSTALLED_APPS in You can include the providers you want to enable (refer here):

    'django.contrib.sites', # Add this

    # Add the following django-allauth apps
    '', # for Google OAuth 2.0
    # ...

At the bottom of, we also need to specify the allauth backend and configuration settings:


Continue to add SITE_ID and specify redirect URL upon successful Google login. You can also add additional configuration settings as shown below:


# Additional configuration settings

Finally, enable email scope to receive user’s email addresses after successful social login:

    'google': {
        'SCOPE': [
        'AUTH_PARAMS': {
            'access_type': 'online',

Go to file of your project directory and add the allauth urls and specify include on top of import. We add a route /accounts that includes all django-allauth URLs. All OAuth operations will be performed under this route.

    'google': {
        'SCOPE': [
        'AUTH_PARAMS': {
            'access_type': 'online',

Configuring Google APIs

To add Google login on your app, you’ll need to set up OAuth application via Google Developers Console.

1. Create New Google APIs project

Head over to Google Developer APIs Console and create a new project:

  • Go to Dashboard, create a New Project
  • Name your new project, preferably your website or app name. User will be able to see this project name when we redirect them to Google login page.
  • Click 'Create' to proceed.

Imgur Image

NOTE: There is a shortcut method by accesing this link to skip Step 2. All you need to do is click the 'Enable the Gmail API button' and it will automatically create a new Google APIs project for you along with the Client ID and Client Secret. That way, you can just proceed to Step 3.

Next, register your app by filling the OAuth consent screen. You only need to provide 'App name', 'User support email' and 'Email addresses' under 'Developer contact information. Click 'Save and Continue' button.

Imgur Image

3. Create New API Credentials

Back to 'Dashboard', go to 'Credentials' on left panel and click 'Create Credentials' button at the top. On the dropdown, choose 'OAuth Client ID' option.

Under 'Authorized JavaScript origins', add the following URIs:

  • http://localhost:8000

Under 'Authorized redirect URIs', add the following URIs:

  • http://localhost:8000/accounts/google/login/callback/

The reason why we have two near-identical copies of URIs is because Django web server can be accessed using either localhost:8000 or Also, should the app be in production, the URIs should be amended to include the domain name instead.

Imgur Image

On the same page (left hand side), you should be able to see your Client ID and Client secret. Copy these two details for the next step.

4. Add social app in Django admin

First, create a superuser by running the following command in a terminal:

$ python3 createsuperuser

Fill in all the necessary details (username, email, password).

Run the app using:

$ python3 runserver

Open or localhost:8000/admin and login to Django Admin. Under Sites, click Add and put either or localhost:8000 as both the Domain name and Display name.

NOTE: It is possible that Django created an '' site for you already (if it's not a new project). You can also amend the existing site created instead of creating a new one.

Imgur Image

Then, under Social Applications, click Add and fill in the details as follows:

  • Provider: Google
  • Name: <APP_NAME>
  • Client id: <CLIENT_ID> (from Step 3)
  • Secret key: <SECRET_KEY> (from Step 3)
  • Sites: Select your Site in 'Available sites' and click the arrow to move it into 'Chosen sites'

Imgur Image

Since you are currently logged in as a superuser, logout and login again using your Google account to test out the authentication.


If you get an error: SocialApp matching query does not exist at, it means that the ID of the site you created in Django admin is not the same as the one in

It is possible that Django created an '' site for you already (if it's not a new project). So if this is the case you will need to delete that entry from the Sites admin page AND change the SITE_ID in to be the correct ID (probably 2 rather than 1).

Consider playing around with the SITE_ID value. For example: SITE_ID = 3, etc.

5. Test social authentication

For the purpose of this project, I have created my own template. However, for quick testing, you can use the following HTML template:

{% load socialaccount %}

    <title>Google Registration</title>

    <h1>My Google Login Project</h1>
    {% if user.is_authenticated %}
    <p>Welcome, {{ user.username }} !</p>

    {% else %}
    <h1>My Google Login Project</h1>
    <a href="{% provider_login_url 'google' %}">Login with Google</a>
    {% endif %}



Now navigate to to see the 'Sign in with Google' page. Try to access the Google login, you should be able to see the redirect screen:

Imgur Image

After successfully signed in with Google, you should be able to see your Google account name on the homepage:

Imgur Image

User Information

After signing in with Google, you can check the user information obtained from Google at:

Google provides little information about its users. To get more user information from Google, your app needs to be verified.

A SocialAccount model instance is available for users who signed up using their social account.

In your template, you can simply write:

  • Avatar URL: {{ user.socialaccount_set.all.0.get_avatar_url }}
  • UID: {{ user.socialaccount_set.all.0.uid }}
  • Date Joined: {{ user.socialaccount_set.all.0.date_joined}}
  • Last Login: {{ user.socialaccount_set.all.0.last_login}}
  • Full Name: {{ }}


While this particular guide only tests the OAuth 2.0 authentication for Google, you can also integrate other OAuth services similarly using django-allauth. Refer to their list of providers for more information.

All in all, I am glad that I managed to figure out how to implement social authentication in Django. It's much easier than I thought it would be and I hope this will be useful for future projects.



No Comments Yet