avatarJen-Hsuan Hsieh (Sean)

Summary

This article provides a guide on how to enable social login for a ReactJS single page application with a Django API server using Facebook as an example.

Abstract

The article is part of a series that aims to build a ReactJS single page application with a Django API server and deploy it on Heroku. The focus of this article is to introduce the process of exchanging the access token from Facebook to JWT for authorization. The process includes creating a new app on Facebook for developers, retrieving the access token from Facebook using a ReactJS component, and setting up the Django/DRF server with required packages and configurations. The article also provides instructions on how to validate the setup using Postman.

Opinions

  • The author believes that enabling social login can improve user experience and security.
  • The author recommends using the ReactJS component for retrieving the access token from Facebook.
  • The author suggests using Postman for validating the setup.
  • The author provides references to other resources for further reading on the topic.
  • The author encourages feedback and subscribing to their Facebook page for more updates.

Build Single page application with React and Django Part 5.1-Exchange Facebook’s access token to JWT from Django/DRF server for Social Login

Introduction

We have mentioned that how to make endpoints with JWT authorization.

Also, we have introduce how to connect ReactJS with Django.

What if we want to enable social login for social media like Facebook?

Agenda

This article will introduce the process to exchange the access token from Facebook to JWT for authorization. It includes the following topics.

  • Overview
  • Get access token from Facebook
  • Set Django/DRF server
  • Validation with Postman

About this series

The target of this series is to build a ReactJS single page application(SPA) with Django API server and deploy on Heroku.

Overview

There are four roles, Django server, ReactJS client, Facebook authorize server, and the user.

Copy right@A Layman

1.Get access token from Facebook

Create a new app from developer.facebook

1.Create a new app on facebook for developers

2.Copy the key and the secret

Copy right@A Layman

Create a ReactJS to retrieve the access token from Facebook

1.Install required packages

npm install -S react-facebook-login

2.Create a ReactJS component

import React from 'react'
import FacebookLogin from 'react-facebook-login';
const SignInForm = () => {
const responseFacebook = (response) => {
        console.log(response);
      }
return (
       <FacebookLogin
        appId="{APP ID}"
        autoLoad={false}
        fields="name,email,picture"
        callback={responseFacebook}/>
    )
}

3.We can use the component to get the access token from Facebook

Copy right@A Layman

2.Set Django/DRF server

1.Install required packages

pip install python-social-auth
pip install social-auth-app-django
pip install social-auth-core

2.Edit settings.py

INSTALLED_APPS = [
    ...
    'social.apps.django_app.default',
    'social_django'
]
AUTHENTICATION_BACKENDS = [
    'social_core.backends.facebook.FacebookOAuth2',
    'django.contrib.auth.backends.ModelBackend',
]
SOCIAL_AUTH_FACEBOOK_KEY = os.getenv("SOCIAL_AUTH_FACEBOOK_KEY")
SOCIAL_AUTH_FACEBOOK_SECRET = os.getenv("SOCIAL_AUTH_FACEBOOK_SECRET")
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
MIDDLEWARE = [
    ...
    'social_django.middleware.SocialAuthExceptionMiddleware'
]

3.Edit serializers.py

class SocialAuthSerializer(serializers.Serializer):
    provider = serializers.CharField(max_length=255, required=True)
    access_token = serializers.CharField(max_length=4096, required=True, trim_whitespace=True)

4.Edit views.py

5.Edit urls.py

from .views import social_auth
urlpatterns = [
    ...
    path("api/social-auth/", social_auth)
]

3.Validation with Postman

  • Set provider as facebook on the body
  • Set Content-type as application/json on the header
Copy right@A Layman

Then we will get JWT!

References

Summary

Thanks for your patient. I am Sean. I work as a software engineer.

This article is my note. Please feel free to give me advice if any mistakes. I am looking forward to your feedback.

Please feel free to clap if this article can help you. Thank you.

You can also subscribe my page on Facebook.

Related topics

How to use the two-way binding in Knout.js and ReactJS?

Learn how to use SignalR to build a chatroom application

My reflection of :

IT & Network:

Database:

Software testing:

Debugging:

DevOps:

Software Development
Django
Oauth2
Facebook
Python
Recommended from ReadMedium