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 awwsome.dev. 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.

Installation

pip install django-allauth

Setup 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', # must
    'allauth', # must
    'allauth.account', # must
    'allauth.socialaccount', # must
    'allauth.socialaccount.providers.google', # 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 settings.py 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.

SITE_ID = 1

AUTHENTICATION_BACKENDS = [
    # Needed to login by username in Django admin, regardless of `allauth`
    'django.contrib.auth.backends.ModelBackend',

    # `allauth` specific authentication methods, such as login by e-mail
    'allauth.account.auth_backends.AuthenticationBackend',
]

ACCOUNT_EMAIL_VERIFICATION = 'none'

LOGIN_REDIRECT_URL = '/'

Now open urls.py of the main project and add this,

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

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

GitHub OAuth

Now open https://github.com/settings/applications/new 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 http://127.0.0.1:8000 because facebook auth only works with localhost that’s why. You can use http://127.0.0.1:8000 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 manage.py migrate

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

python manage.py createsuperuser

Now run the server using python manage.py 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 http://127.0.0.1:8000 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]
  3. Client id: [YOUR GITHUB OAUTH APP CLIENT ID]
  4. Secret key: [YOUR GITHUB OAUTH APP CLIENT SECRET]
  5. Sites: localhost:8000 or 127.0.0.1:8000 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 :)