episyche logo

Episyche

React/Single sign-on (SSO)/

How to configure Facebook SSO in Django Rest Framework with React?

Published on

How to configure Facebook SSO in Django Rest Framework with React?
Facebook SSO enables existing Facebook and Messenger users to easily and effortlessly sign on to other application integrations. In this tutorial, we will learn to configure the Facebook SSO(Social Login) with Django and React.

Introduction :

Django (Django rest Framework):

Django REST framework (DRF) is a powerful and flexible toolkit for building Web APIs.Its main benefit is that it makes serialization much easier. Django REST framework is based on Django's class-based views, so it's an excellent option if you're familiar with Django. To know more about relative links click here

Node.js :

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on a JavaScript Engine and executes JavaScript code outside a web browser. and NPM is a package manager for Node. js packages. The NPM program is installed on your computer when you install Node.js. To know more about relative links click here

React:

ReactJS is much easier to learn and use. ReactJS is a free and open-source front-end JavaScript library for building user interfaces based on UI components. to create interactive applications for mobile, web, and other platforms. To know more about relative links click here

Single sign-on(SSO):

Single sign-on (SSO) is a time-saving and highly secure user authentication process. SSO lets users access multiple applications with a single account and sign out instantly with one click.

How it Works:


how it's work?

Flow Diagram:



flow diagram

Steps:

Step 1: Create a Facebook client Id and client Secret

Check out this tutorial, to create a client Id and client secret.

Step 2: Set up a React project

Install Nodejs

Before we create React App we need to install node.js.

if you already installed node.js please ignore this step 2 and go to step 3

Step 3: Create a React-app

After installing Node.js open the terminal or command prompt and create the React app with the following commands

1npx create-react-app frontend

A sample screenshot of React app files is shown below.


Screenshot1

Step 4: Configure the React project to establish a connection with Facebook.

After creating the React app then install the react-facebook-login commands

1npm install react-facebook-login --legacy-peer-deps

In src/App.js, add the following code.

1import FacebookLogin from 'react-facebook-login'; 2import './App.css'; 3 4function App() { 5  function responseFacebook(e) { 6    fetch("http://127.0.0.1:8000/facebook/", 7      { 8        method: "POST", 9        body: JSON.stringify({ "auth_token": e['accessToken'] }), 10        headers: { 11          'Content-Type': 'application/json; charset=utf-8' 12        } 13      }) 14      .then((res) => res.json()) 15      .then((res) => { 16        document.getElementById("email_id").innerText = res['email'] 17        document.getElementById("auth-token").innerText = res['tokens'] 18      }) 19  } 20  return ( 21    <div className="App"> 22      <div className="facebook"> 23 24        <FacebookLogin 25          appId="380405740733503" 26          autoLoad={true} 27          fields="name,email,picture" 28          callback={responseFacebook} 29 30        /> 31      </div> 32      <div className='show-info'> 33        <div> 34          <label>Email Id:</label> 35          <label id='email_id'></label> 36        </div> 37        <div> 38          <label>Auth Token:</label> 39          <label id='auth-token'></label> 40        </div> 41      </div> 42    </div> 43  ); 44} 45 46export default App;

Sample code for the App.js file can be found in the following GitHub URL.

You can run your app via CLI with the following command and view it in your browser:

1npm start

Front end results

Step 5: Set up a Django project.

1: Install Python

Python3.6 or above is needed to create a Django project, therefore please install python and proceed to the next step.

2: Install Django:

Install the Django framework using the following command.

1pip install django

3: Create a Django Project:

Create a Django project using the below command.

1django-admin startproject backend

Step 6: Create a Django App:

After creating the Django project, then create the Django app

1python manage.py startapp accounts

A sample screenshot of Django project files is shown below.


Screenshot2

Step 7: Configure the Django project to establish a connection with Facebook.

1: Install djangorestframework:

In this blog, we are using the Django Rest framework python library to create APIs, therefore the please install same to proceed further.

1pip install djangorestframework

2: Install django-cors-headers:

django-cors-headers the library needs to establish a connection from the React frontend to Django API, hence please install the same using the below command.

1pip install django-cors-headers

3: Install facebook-sdk:

1pip install facebook-sdk==3.1.0
  • In backend/settings.py, add the following piece of code:

1CORS_ORIGIN_ALLOW_ALL = True
1MIDDLEWARE = [ 2 'django.middleware.security.SecurityMiddleware', 3 'django.contrib.sessions.middleware.SessionMiddleware', 4 'django.middleware.common.CommonMiddleware', 5 'django.middleware.csrf.CsrfViewMiddleware', 6 'django.contrib.auth.middleware.AuthenticationMiddleware', 7 'django.contrib.messages.middleware.MessageMiddleware', 8 'django.middleware.clickjacking.XFrameOptionsMiddleware', 9 'corsheaders.middleware.CorsMiddleware', # <<< newly added line 10] 11

Sample code for the /accounts/settings.py can be found in the following GitHub URL.

  • In your project, go to your /accounts directory and create a file called serializers.py.



Screenshot3

In accounts/serializers.py, add the following code.

1from django.conf import settings 2from rest_framework import serializers 3from library.sociallib import facebook 4from library.register.register import register_social_user 5from rest_framework.exceptions import* 6 7class FacebookSocialAuthSerializer(serializers.Serializer): 8 """Handles serialization of facebook related data""" 9 auth_token = serializers.CharField() 10 11 def validate_auth_token(self, auth_token): 12 user_data = facebook.Facebook.validate(auth_token) 13 14 try: 15 user_id = user_data['id'] 16 email = user_data['email'] 17 name = user_data['name'] 18 provider = 'facebook' 19 except: 20 21 raise serializers.ValidationError( 22 'The token is invalid or expired. Please login again.' 23 ) 24 return register_social_user( 25 provider=provider, user_id=user_id, email=email, name=name)

Sample code for the /accounts/serializers.py can be found in the following GitHub URL.

  • Update the accounts/views.py file with the following code.

1from rest_framework.generics import GenericAPIView 2from .serializers import* 3from rest_framework.response import Response 4from rest_framework import status 5from rest_framework.permissions import AllowAny 6from rest_framework.decorators import permission_classes 7 8@permission_classes((AllowAny, )) 9class FacebookSocialAuthView(GenericAPIView): 10 11 serializer_class = FacebookSocialAuthSerializer 12 13 def post(self, request): 14 """ 15 POST with "auth_token" 16 Send an access token as from facebook to get user information 17 """ 18 19 serializer = self.serializer_class(data=request.data) 20 serializer.is_valid(raise_exception=True) 21 data = ((serializer.validated_data)['auth_token']) 22 return Response(data, status=status.HTTP_200_OK)

Sample code for the /accounts/views.py can be found in the following Github URL.

 

  • Create the sociallib and register directory in the following hierarchy.

    library → socaillib

    library → register.

  • After that, please create a facebook.py the file inside the sociallib directory



Screenshot5

In library/sociallib/facebook.py, add the following snippet.

1import facebook 2 3class Facebook: 4 """ 5 Facebook class to fetch the user info and return it 6 """ 7 8 @staticmethod 9 def validate(auth_token): 10 """ 11 validate method Queries the facebook GraphAPI to fetch the user info 12 """ 13 try: 14 graph = facebook.GraphAPI(access_token=auth_token) 15 profile = graph.request('/me?fields=name,email') 16 return profile 17 except: 18 return "The token is invalid or expired."

The sample code for the library/sociallib/facebook.py file can be found in the following Github URL.

  • Navigate to the library/register directory and create a file called register.py



Screenshot4

In library/register/register.py, add the following code.

1from rest_framework.authtoken.models import Token 2 3from accounts.models import User 4from django.conf import settings 5from rest_framework.exceptions import AuthenticationFailed 6 7 8def register_social_user(provider, user_id, email, name): 9 filtered_user_by_email = User.objects.filter(email=email) 10 11 if filtered_user_by_email.exists(): 12 if provider == filtered_user_by_email[0].auth_provider: 13 new_user = User.objects.get(email=email) 14 15 registered_user = User.objects.get(email=email) 16 registered_user.check_password(settings.SOCIAL_SECRET) 17 18 Token.objects.filter(user=registered_user).delete() 19 Token.objects.create(user=registered_user) 20 new_token = list(Token.objects.filter( 21 user_id=registered_user).values("key")) 22 23 return { 24 'username': registered_user.username, 25 'email': registered_user.email, 26 'tokens': str(new_token[0]['key'])} 27 28 else: 29 raise AuthenticationFailed( 30 detail='Please continue your login using ' + filtered_user_by_email[0].auth_provider) 31 32 else: 33 user = { 34 'username': email, 'email': email, 35 'password': settings.SOCIAL_SECRET 36 } 37 user = User.objects.create_user(**user) 38 user.is_active = True 39 user.auth_provider = provider 40 user.save() 41 new_user = User.objects.get(email=email) 42 new_user.check_password(settings.SOCIAL_SECRET) 43 Token.objects.create(user=new_user) 44 new_token = list(Token.objects.filter(user_id=new_user).values("key")) 45 return { 46 'email': new_user.email, 47 'username': new_user.username, 48 'tokens': str(new_token[0]['key']), 49 }

The sample code for the library/register/register.py file can be found in the following Github URL.

 

  • Add the accounts/urls.py file with the following content.

1from django.urls import path 2from .views import* 3 4urlpatterns = [ 5 path('facebook/', FacebookSocialAuthView.as_view()), 6]

An example screenshot of the Django project is shown below.


Screenshot5


The sample code for the accounts/urls.py file can be found in the following Github URL.

  • Finally, Run the Django application using the following command.

1 python manage.py runserver

 

A Sample Github repo, with all the required configurations, is given below.

Result:

After finishing all the steps mentioned above, please go to the web browser, and try to access the React application (i.e http://localhost:3000). An example output screenshot is given below.


Results2

Comments