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
- Homepage URL: http://localhost:8000/
- Authorization callback URL: http://localhost:8000/accounts/github/login/callback/
Then click on Register application
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.
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
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.
- Provider: GitHub
- Name: [Whatever your want]
- Client id: [YOUR GITHUB OAUTH APP CLIENT ID]
- Secret key: [YOUR GITHUB OAUTH APP CLIENT SECRET]
- Sites: localhost:8000 or 127.0.0.1:8000 or your domain
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.
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 :)
- Official doc: GitHub auth
- Previous tutorial: Facebook Authentication in Django using Django-allauth
- Django-allauth tutorial: https://www.codesnail.com/django-allauth-tutorial/