GitHub Authentication in Django using Django-allauth

Hello internet programmer. Let’s add GitHub authentication in Django app using Django-allauth package. Even I used GitHub auth in my project So let’s jump on the code.

Here we are continuing the last tutorial and we added local authentication and Google authentication, and Facebook authentication but don’t worry you can still follow this for your project directly.


pip install django-allauth

Setup django-allauth


    'django.contrib.sites', # must
    'allauth', # must
    'allauth.account', # must
    'allauth.socialaccount', # must
    '', # google provider
    'allauth.socialaccount.providers.facebook', # facebook provider
    'allauth.socialaccount.providers.github', # new (github provider)

You can see we include GitHub providers for authentication. In the previous tutorial, we added a Google and Facebook provider.

Then at the bottom of we need to specify that we’re using the allauth backend, add a SITE_ID since allauth uses this, and configure a redirect to the homepage upon successful login.


    # Needed to login by username in Django admin, regardless of `allauth`

    # `allauth` specific authentication methods, such as login by e-mail



Now open of the main project and add this,

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

urlpatterns = [
    path('accounts/', include('allauth.urls')),

GitHub OAuth

Now open to get configure the OAuth app in GitHub. You will get a client ID and secret key.

Fill up the details and for

Then click on Register application

Registering a new OAuth application in GitHub, GitHub Authentication in Django app with django-allauth

Registering a new OAuth application in GitHub

See, I’m using http://localhost:8000 not because facebook auth only works with localhost that’s why. You can use if you’re only using GitHub. But if you are using a domain then this problem will not happen.

Okay after on the next page you will see the Client ID and for the Client Secret (Secret key) you need to click on Generate a new client secret button.

github oauth client secret and client id page, django-allauth github authentication

Now you have Client ID and Client secret

Add a social app in Django admin

First, run migrate command to migrate all django-allauth stuff

python migrate

assuming you’ve already created a superuser if not then create using,

python createsuperuser

Now run the server using python runserver and open admin. Go to this page http://localhost:8000/admin/sites/site/1/change/ and make these changes, if you’re currently in production, you can use the IP or domain name of your page

site settings in django admin for django-allauth github authentication

site settings

You are totally fine with okay

Then, click on Social Applications click Add and fill in the details as follows, and save it.

  1. Provider: GitHub
  2. Name: [Whatever your want]
  5. Sites: localhost:8000 or or your domain

Add GitHub in social applications in Django admin. Github login in django

Add GitHub in social applications in Django admin

Now log out yourself because you logged in as a superuser and open http://localhost:8000/accounts/login/ and you can see GitHub login option.

GitHub login option on the login page, github oauth in django

GitHub login option on the login page

If you have customized django-allauth templates then you can directly use this code in your template.

{% load socialaccount %}

<h1>Django Allauth Tutorial</h1>

<a href="{% provider_login_url 'github' %}">Sign Up with GitHub</a>

Okay, that’s it. Happy Coding :)