Django Social Login Authentication Example

In this article we are going to talk about Django Social Login Authentication Example, especially we are going to create Django login authentication with Facebook, Github, Linkedin.

 

 

 

Check the complete video for this article

 

 

 

 

Also you can read more django articles

1: Django Pagination Complete Example

2: Django Sending Email to Gmail Account 

3: Build News Application in Django 

4: Django Aggregation Example

5: Django User Authentication Framework 

 

 

 

OK first of all you need to create a new project in django.

 

 

 

After that we are going to create an app. make sure that you have changed the directory to your project.

 

 

 

Now you need to migrate your project

 

 

 

Also you need to create a super user, because we need to use that for our login system.

 

 

OK first of all you need to install this package for working social authentication.

 

 

 

Add the application to INSTALLED_APPS setting, also you need to add your newly created app in your settings.py.

 

 

 

Add desired authentication backends to Django’s AUTHENTICATION_BACKENDS setting: basically we have added Facebook, Github and Linkedin authentication backends. Don’t miss django.contrib.auth.backends.ModelBackend if using django.contrib.auth application or users won’t be able to login by username / password method.

 

 

 

Also you need to update the context_processors inside TEMPLATE:

 

 

 

 

Make sure after adding the models sync the database to create needed models once you added social_django to your installed apps:

 

 

 

 

Also you need to create a new file in your created app and called urls.py. and add this code

 

 

 

Make sure that you have linked this urls.py in your main project urls.py like this.

 

 

 

Also in your settings.py you need to add this because of login redirect.

 

 

 

OK now you need to create a templates folder in your django project, after that create another folder in your templates folder called registration, and add these files.

Project Structure
Project Structure

 

 

 

The first one is login.html, this file should be in your registration folder.

 

 

 

 

If you see we have added the links for our facebook, github and linkedin authentication in our login.html

 

 

 

 

This is base.html, it should be out side the registration folder.

 

 

 

 

And this is our home.html for redirecting in successful login.

 

 

 

 

 

Facebook Authentication 

 

Go to Facebook Developer click on My Apps and then Add a New App.

 

Django Social Login Authentication Example
Django Social Login Authentication Example

 

 

 

 

And give a name for your app.

Facebook Create App
Facebook Create App

 

 

 

After creating of app, click on the settings and after that basic, and in the App Domains you need to just add localhost.

Facebook App
Facebook App

 

 

 

Also you need to setup Facebook Login in your dashboard, after that choose Web and in the Site URL you need to just add this .

Site URL
Site URL

 

 

 

Make sure that after creating App you have copied the secret key and app id, because we will need that for login process.

 

 

 

 

Github Authentication 

 

For github you need to first login in to your account, and after that Register a new Oauth application with this information.

Django Social Login Authentication Example
Django Social Login Authentication Example

 

After that click on Register application, make sure that after creating App you have copied the secret key and app id, because we will need that for login process.

 

 

 

Likendin Authentication 

 

First of all you need to open Linkedin For Developers, and create a new app. after that you need to give a name for your app, company name and also a logo. these are required. and after creation of app you need to click on the Auth. the important point is giving Redirect URL for OAuth 2.0 Settings . you need to add that section.

 

 

 

Image for the setup

Linkedin App
Linkedin App

 

Make sure that after creating App you have copied the secret key and app id, because we will need that for login process.

 

 

 

 

We have already added the links for these three login authentication in our login.html file like this.

 

 

 

 

So now you need to open your setttings.py and add the secret key and app id for these auths like this.

 

 

 

 

If you run the code this will be the result for facebook, and you can run for github and linkedin also.

Django Social Login Authentication Example
Django Social Login Authentication Example

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×