I'm looking to allow users to sign into my web app either by creating a profile (done through django.contrib.auth
), or using LinkedIn (done through python-social-auth
).
Relevant file hierarchy is as follows:
.
├── ./app
│ ├── ./app/admin.py
│ ├── ./app/forms.py
│ ├── ./app/__init__.py
│ ├── ./app/models.py
│ ├── ./app/serializers.py
│ ├── ./app/templates
│ │ ├── ./app/templates/app
│ │ │ ├── ./app/templates/app/app.js
│ │ │ ├── ./app/templates/app/create_profile.html
│ │ │ ├── ./app/templates/app/index.html
│ │ │ ├── ./app/templates/app/login.html
│ │ │ ├── ./app/templates/app/profile.html
│ │ │ ├── ./app/templates/app/signup.html
│ │ │ └── ./app/templates/app/userhome.html
│ ├── ./app/tests.py
│ ├── ./app/urls.py
│ ├── ./app/views.py
├── ./proj
│ ├── ./proj/settings.py
│ ├── ./proj/urls.py
│ ├── ./proj/wsgi.py
└── ./manage.py
Thus far, I've added the following entries into my settings.py
:
INSTALLED_APPS = (
'django.contrib.auth',
# Entries for other purposes...
# ...
# ...
'social.apps.django_app.default'
)
AUTHENTICATION_BACKENDS = (
'social.backends.linkedin.LinkedinOAuth',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_LINKEDIN_KEY = '*secret*'
SOCIAL_AUTH_LINKEDIN_SECRET = '*secret*'
SOCIAL_AUTH_LINKEDIN_SCOPE = [ 'r_basicprofile' ]
LOGIN_REDIRECT_URL = '/userhome'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Entries for other purposes...
# ...
# ...
'social.apps.django_app.context_processors.backends',
'social.apps.django_app.context_processors.login_redirect',
],
'debug': DEBUG,
},
},]
In proj/urls.py
, I've added the following entry:
url('', include('social.apps.django_app.urls', namespace='social')),
On my login.html
page, I've added a link:
<br>Or, <a href="/login/linkedin">sign in with LinkedIn.</a>
My login view in app/views.py
looks like this:
def login (request):
form = LoginForm()
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
auth_login(request, user)
return redirect('/userhome/')
else:
messages.error(request, 'Invalid login or password')
else:
messages.error(request, 'Missing username or password')
return render(request, 'app/login.html', {'form': form})
Currently, my "sign in with LinkedIn" link fails to get picked up by the python-social-auth
backend. As pointed out in this tutorial, I've confirmed that python-social-auth
's backend is working: I can see empty tables on the site's admin page.
I've read the following tutorials or SO entries to try and answer my question:
- Using python-social-auth with linkedin
- Python Social Auth Django template example
- https://python-social-auth.readthedocs.io/en/latest/configuration/django.html
- https://github.com/omab/python-social-auth/blob/master/examples/django_example/example/settings.py
Any idea what I'm missing?