Add social authentication with python-social-auth and social-auth-app-django. You will need to setup app id & secret in a ./local_settings.py file to use them

This commit is contained in:
Michael Hall 2017-12-27 15:00:49 -05:00
parent 186e9eac5b
commit a17a5b7894
5 changed files with 53 additions and 3 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
*.sqlite*
*.pyc
*local_settings.*

View file

@ -1,3 +1,13 @@
{% if request.user.is_authenticated %}
Welcome {{ request.user.username }}
{% else %}
Login:
<a href="{% url 'social:begin' 'google-oauth2' %}">Google</a>
<a href="{% url 'social:begin' 'facebook' %}">Facebook</a>
<a href="{% url 'social:begin' 'twitter' %}">Twitter</a>
<a href="{% url 'social:begin' 'github' %}">GitHub</a>
<br>
{% endif %}
<center>{% if events_list %}
<table border="0" width="960px">
{% for event in events_list %}

View file

@ -11,6 +11,11 @@ https://docs.djangoproject.com/en/2.0/ref/settings/
"""
import os
try:
from local_settings import *
except:
print("WARNING: You should create a local_settings.py to store local and secret data.")
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@ -42,9 +47,24 @@ INSTALLED_APPS = [
'django.contrib.sites',
'rest_framework',
'social_django',
'events',
]
LOGIN_URL = 'login'
LOGOUT_URL = 'logout'
LOGIN_REDIRECT_URL = 'home'
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'social_core.backends.open_id.OpenIdAuth',
'social_core.backends.google.GoogleOAuth2',
'social_core.backends.github.GithubOAuth2',
'social_core.backends.twitter.TwitterOAuth',
'social_core.backends.facebook.FacebookOAuth2',
'social_core.backends.yahoo.YahooOpenId',
)
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
@ -53,6 +73,8 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'social_django.middleware.SocialAuthExceptionMiddleware',
]
ROOT_URLCONF = 'get_together.urls'
@ -68,7 +90,9 @@ TEMPLATES = [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
},
},
]

View file

@ -14,11 +14,13 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
from events import views
urlpatterns = [
path('admin/', admin.site.urls),
path('searchables/', views.searchable_list),
path('events/', views.events_list),
path('events/', views.events_list, name='home'),
path('oauth/', include('social_django.urls', namespace='social')),
]

View file

@ -1,4 +1,17 @@
certifi==2017.11.5
chardet==3.0.4
defusedxml==0.5.0
Django==2.0
djangorestframework==3.7.7
idna==2.6
oauthlib==2.0.6
PyJWT==1.5.3
python3-openid==3.1.0
pytz==2017.3
requests==2.18.4
requests-oauthlib==0.8.0
simplejson==3.13.2
six==1.11.0
social-auth-app-django==2.1.0
social-auth-core==1.6.0
urllib3==1.22