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
- Install django-allauth package
pipenv install django-allauth
ORpoetry add django-allauth
- 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"
-
Move templates to
account
directorymv templates/registration/ templates/account
(N.B.: NO 's' at end of account) -
Update URLs
# config/urls.py
urlpatterns = [
path("admin/", admin.site.urls),
path("accounts/", include("allauth.urls")), # new
path("", include("djangoblog.urls")),
]
-
Update any templates containing 'login' or 'logout' to 'account_login' 'account_logout' etc...
-
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
-
Copy Client ID and Client secret(you won't see it again!!!) Click
Update Application
-
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
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>
- Override the 'You are about to sign in using a third party account from GitHub.' template
mkdir templates/socialaccount
- Create new
login.html
template
- Test & verify working