Updated at Mar 2, 2017:
This post was updated to work with the newest version of the social-auth-app-django library.
In this tutorial we will implement Facebook, Twitter and GitHub authentication using the social-auth-app-django
library. They support several other services and the process should be somewhat similar. The social-auth-app-django
library have several customization options, which sometimes makes it challenging to get started. So for this tutorial
I will guide you through the mandatory steps where in the end you will have a fully functional social authentication.
Installation
As usual, go for pip:
Add the social_django to your INSTALLED_APPS:
Migrate the database:
The library will automatically handle authentication tokens and all the required information to deal with OAuth and
OAuth2. Generally speaking, you won’t need to handle it manually nor access the user’s social profile.
Configuration
Update the MIDDLEWARE_CLASSES by adding the SocialAuthExceptionMiddleware to the end of it:
Now we update the context_processors inside TEMPLATE:
Add the AUTHENTICATION_BACKENDS:
So, as you can see, here at this point we will need to specify the correct backends, depending on your needs. Since
here in this example we will be working with GitHub, Twitter and Facebook I just added those three
backends.
Update the urls.py add the social-auth-app-django urls:
urls.py
Let’s set the default values for the LOGIN_URL, LOGOUT_URL and the LOGIN_REDIRECT_URL.
The LOGIN_REDIRECT_URL will be used to redirect the user after authenticating from Django Login and Social Auth.
As we are using the built-in login and logout views, let’s just create the home view inside the core app:
views.py
home.html
We are good to go. For each social network you will need to register your app in their system. So from now on the steps
will vary on the service provider. Let’s tackle them one at a time. First one: GitHub.
GitHub Authentication
Log in to your GitHub account, go to Settings. In the left menu you will see Developer settings. Click on
OAuth applications.
In the OAuth applications screen click on Register a new application. Or simply click on the link below:
The important step here is the Authorization callback URL. Notice that I’m putting a localhost URL.
http://localhost:8000/oauth/complete/github/.
After you create the app, you will be redirected to this screen:
PS: Keep this information safe. I’m putting it here just so it is more explicit in the example. For the running
application you will find in the end of this tutorial it uses different credentials.
Anyhoo, let’s grab the information we need:
Now we update the settings.py:
PS 2: It is not a good idea to keep this kind of information directly in the settings.py. Please refer to this
article Package of the Week: Python Decouple to learn how to
separate configuration from settings.
Login View
Let’s create a Login View that can both handle Django Login and the GitHub Login. The default login view look for a
template in registration/login.html. Let’s create it:
registration/login.html
Note that the important part to start the GitHub authentication is {%url'social:begin''github'%}.
At the moment our login page should look like this:
A good thing about the social login is that the process to both Login or Signup is pretty much the same. Meaning that
the user without having an account in your application, he/she can login. If there is no account matching the
username/email, an account will automatically be created.
Let’s try our new Login with GitHub link:
Click on Authorize application
And there you go! An account was just created and is signed in!
There is a small caveat in cases like this: the user have created an account but have no password. We will address this
issue later on.
Add the details just like you did with the GitHub App:
For Twitter we will use the http://localhost:8000/oauth/complete/twitter/ callback url.
Go to Permissions tab and change the permissions to Read Only:
The less permissions we have better. This way we make our users more comfortable.
Now go to Keys and Access Tokens tab and grab the Consumer Key (API Key) and Consumer Secret (API Secret) and
update the settings.py:
To enable the Twitter login, simply update the registration/login.html template:
Let’s try it:
And there you go!
See how it created a new user with a unglier name? vitorfs742c692ac9494871. That’s because both my username on
GitHub and on Twitter is vitorfs. And just from the username we can’t guarantee that it’s the same user, the
social-auth-app-django handles it by creating a new user and appending an arbitrary string to the end of it.
That’s okay for now! I will show later on where you can work out this behaviour.
Click on Create App ID. On the next screen click on Skip Quick Start.
From the initial page, grab the App ID and App Secret (click on the Show button to get it as plain text):
Now go to Settings / Basic. First click on the button + Add Platform and add a website. For the Site URL
put http://localhost:8000 and then in the App Domains put just localhost, like the picture below:
PS: For Facebook we can’t use 127.0.0.1 as the callback. Instead use localhost. Or add a fake host in your local
hosts. Usually you can find it at /etc/hosts.
Save changes and we are good to go. Now update the registration/login.html file with the Facebook link:
Let’s try it now:
Social Auth Management Page
We can now add a Settings page in our application, where the user can manage the social auth logins. It’s a good way
for the user authorize new services or revoke the access.
Basically this page will be responsible for:
Disconnecting the user from the social networks
Control if the user can disconnect (that is, the user have defined a password, so he/she won’t be locked out of the system)
Provide means to connect to other social networks
urls.py
A password view will be very important in this case!
Now update the project settings.py file with the following configurations:
settings.py
views.py
Please note that this password view handle both Set password and Change password.
settings.html
If you look close, it’s just a repetitive work. Basically we are showing if the account is connected with one of the
providers, and giving the option to disconnect (whenever it is possible).
Finally the password view, in case you are wondering what it looks like:
password.html
The /settings/ page will look like this:
In case I disconnect from Twitter for example, the GitHub Disconnect button will be disabled:
This way we force the user to set a password before disconnecting from GitHub, avoiding her/him to be locked out.