episyche logo

Episyche

React/Single sign-on (SSO)/

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

Published on

How to configure GitHub SSO in Django Rest Framework with React?
Single Sign On (SSO) gives your users convenient but secure access to all their web applications with a single set of credentials. This guide explains how we can configure Github SSO(Social Login) in the Django Rest Framework backend with React.

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 Github 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 Github.

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

1npm i react-login-github

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

1import './App.css'; 2import LoginGithub from 'react-login-github'; 3import github from "./github.png" 4 5function App() { 6 function onSuccess(e) { 7 fetch("http://127.0.0.1:8000/github/", { 8 method: "POST", 9 body: JSON.stringify({ "auth_token": e.code }), 10 headers: { 11 'Content-Type': 'application/json; charset=utf-8' 12 } 13 }) 14 .then((res) => res.json()) 15 .then((response) => { 16 document.getElementById("email_id").innerText = response['email'] 17 document.getElementById("auth_token").innerText = response['tokens'] 18 }) 19 20 } 21 function onFailure(e) { 22 alert(e) 23 } 24 return ( 25 <div className="App"> 26 <LoginGithub 27 clientId="Github client Id" 28 onSuccess={onSuccess} 29 onFailure={onFailure} 30 31 className="github"> 32 <img src={github} alt="Sign in with github" className='github-image' ></img> 33 <h2 className='github-name'>Sign in with github</h2> 34 35 </LoginGithub> 36 <div className="show-user_info"> 37 <div > 38 <label className="info">Email Id:</label> 39 <label id='email_id'></label> 40 </div> 41