Enabling GitHub OAuth in Django

Django allows integration with GitHub OAuth for user authentication, making it easy for users to log in using their GitHub credentials. This tutorial demonstrates how to implement GitHub OAuth in a Django application using the django-allauth library.

Setting Up GitHub OAuth Application

First, create a GitHub OAuth application:

  1. Go to GitHub Developer Settings
  2. Create a new OAuth App with these URLs:
  • Homepage URL: http://127.0.0.1:8000/
  • Authorization callback URL: http://127.0.0.1:8000/accounts/github/login/callback/

Save the Client ID and Client Secret for later use.

Installing Required Dependencies

Install the django-allauth library ?

pip install django-allauth

Django Configuration

Settings Configuration

Add the following configuration to your settings.py file ?

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    
    # Required for django-allauth
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.github',
    
    # Your app
    'githubAuthentication'
]

SITE_ID = 1

AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend'
]

LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'

URL Configuration

Configure the project's main urls.py file ?

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

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

Admin Configuration

After running migrations, configure the social application:

  1. Run python manage.py migrate
  2. Go to http://localhost:8000/admin/
  3. Navigate to Social Applications
  4. Create a new application with:
    • Provider: GitHub
    • Name: GitHub OAuth
    • Client ID: Your GitHub Client ID
    • Secret key: Your GitHub Client Secret
    • Sites: Select example.com

Creating Templates and Views

Template

Create templates/home.html ?

<!DOCTYPE html>
<html>
<head>
    <title>GitHub OAuth Demo</title>
</head>
<body>
    {% load socialaccount %}
    
    {% if user.is_authenticated %}
        <h1>Welcome, {{ user.username }}!</h1>
        <p>You are logged in via GitHub.</p>
        <a href="{% url 'account_logout' %}">Logout</a>
    {% else %}
        <h1>Login with GitHub</h1>
        <a href="{% provider_login_url 'github' %}">Login with GitHub</a>
    {% endif %}
</body>
</html>

Views

Create your view in views.py ?

from django.shortcuts import render

def home(request):
    return render(request, "home.html")

App URLs

Configure your app's urls.py ?

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name="home"),
]

Testing the Implementation

Start your development server and visit http://localhost:8000/. You should see the login page with a GitHub OAuth option. After clicking the login link, users will be redirected to GitHub for authorization, then back to your application upon successful authentication.

Conclusion

GitHub OAuth integration in Django is straightforward using django-allauth. This setup provides a secure authentication method while offering users a convenient login experience. Remember to update your GitHub OAuth app settings when deploying to production.

Updated on: 2026-03-26T00:39:17+05:30

795 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements