Skip to content

Instantly share code, notes, and snippets.

@kevinbowen777
Last active June 19, 2022 10:11
Show Gist options
  • Save kevinbowen777/e5cc993ea8223abbfeee6371faf35be5 to your computer and use it in GitHub Desktop.
Save kevinbowen777/e5cc993ea8223abbfeee6371faf35be5 to your computer and use it in GitHub Desktop.
django-allauth social login integration

Social Login integration with Django & allauth - 20220419


References: Source code: https://github.com/pennersr/django-allauth Documentation: http://django-allauth.readthedocs.io/en/latest/providers.html Tutorial: https://learndjango.com/tutorials/django-allauth-tutorial


  1. Install django-allauth package
  • pipenv install django-allauth OR
  • poetry add django-allauth
  1. Add to INSTALLED_APPS
# config/settings.py
INSTALLED_APPS = [
    "django.contrib.admin,"
    ...,
    "django.contrib.sites",  # new

    # Third-party packages
    "allauth",  # new
    "allauth.account",  # new
    "allauth.socialaccount",
    "allauth.socialaccount.providers.github",  # new
    # Local apps...,
]

# At the bottom of the config, add:
SITE_ID = 1
LOGIN_REDIRECT_URL = "home"
ACCOUNT_LOGOUT_REDIRECT_URL = "home"
AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
)
ACCOUNT_EMAIL_VERIFICATION = "none"
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
  1. Move templates to account directory mv templates/registration/ templates/account (N.B.: NO 's' at end of account)

  2. Update URLs

# config/urls.py
urlpatterns = [                                                                 
    path("admin/", admin.site.urls),                 
    path("accounts/", include("allauth.urls")),  # new                                 
    path("", include("djangoblog.urls")),                                       
]     
  1. Update any templates containing 'login' or 'logout' to 'account_login' 'account_logout' etc...

  2. Register a new GitHub OAuth application at https://github.com/settings/applications/new

Application Name: Django Blog Application
Homepage URL: http://127.0.0.1:8000
Application Description:
    A basic poll application built with Django web framework
Authorization callback URL:
    http://127.0.0.1:8000/accounts/github/login/callback/
Enable Device Flow: Leave unchecked

Click register button

  1. Copy Client ID and Client secret(you won't see it again!!!) Click Update Application

  2. login to http://127.0.0.1/admin

  • Select sites
  • Set Domain name to 127.0.0.1
  • Go back to admin page
  • Select Social Applications enter the ID & secrets obtained in Step 7
  • Add our site to Chosen Sites
  1. Edit templates/account/login.html
  • Add at/near top of page: {% load socialaccount %}
  • Add button(s) after </form>:
<p class="text-center text-muted">OR</p>                                      
<a class="btn btn-dark" href="{% provider_login_url 'github' %}">Continue with GitHub</a>
  1. Override the 'You are about to sign in using a third party account from GitHub.' template
  • mkdir templates/socialaccount
  • Create new login.html template
  1. Test & verify working
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment