Introduction
In this article, we will learn the step-by-step process of allowing users to log in to an application with Facebook using ReactJS. Logging in with Facebook makes it safe and easy for users to use applications. When a user clicks on the Login with Facebook button, the user is navigated Facebook to give the app permission. In response, the user receives a token key and other personal details.
Prerequisites
- Basic knowledge of ReactJS and Web API
- Visual Studio and Visual Studio code
- SQL Server Management Studio
Topics Covered in this Article,
- Create a ReactJS Project
- Install rreact-facebook-login React plugin
- Install Axios and Bootstrap
- Add React Router
- Install Bootstrap and React strap
- Create a Google App and get client Id
- Create a database and table
- Create a Web API Project
Create a ReactJS project by using the following command
- npx create-react-app sociallogin
Open the newly-created project in Visual Studio code and install react-google-login React plugin using the following command
- npm install react-facebook-login --save
Now install Bootstrap in this project by using the following command.
- npm install --save bootstrap
Now, open the index.js file and add import Bootstrap.
- import 'bootstrap/dist/css/bootstrap.min.css';
Now install the Axios library by using the following command. Learn more about Axios
- npm install --save axios
Create a Facebook App and Get App Id
Go to Facebook developer page and create a new App,

Click on create App ID

Click on Facebook login setup

Click on Web

Enter Site url and click on save, now go to setting, and copy app id
Now, in Visual Studio code, go to src folder and create a new folder and inside this folder add 2 new components
- Loginbyfacebook.js
- Dashboard.js
Add Routing in ReactJS
Install react-router-dom package by using the following command
- npm install react-router-dom --save
Open app.js file and imports of Router and Route (react-router-dom) and 2 components.
- import Logintbygoogle from './Logintbygoogle'
- import Dashboard from "./Dashboard";
- import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
Add the following code in app.js file,
- import React from 'react';
- import logo from './logo.svg';
- import './App.css';
- import Logintbyfacebook from './Logintbyfacebook'
- import Dashboard from "./Dashboard";
- import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
- function App() {
- return (
- <>
- <div className="App">
- <Router>
- <div className="container">
- <Switch>
- <Route exact path='/' component={Logintbyfacebook} />
- <Route path='/Dashboard' component={Dashboard} />
- </Switch>
- </div>
- </Router>
- </div>
- </>
- );
- }
- export default App;
Now, open the Loginbyfacebook.js file and add the following code.
- import React, { Component } from 'react'
- import FacebookLogin from 'react-facebook-login';
- import axios from 'axios'
- export class Logintbyfacebook extends Component {
- constructor(props) {
- super(props);
- this.state = {
- };
- }
- signup(res) {
- const responseFacebook = {
- Name: res.name,
- email: res.email,
- token: res.accessToken,
- Image: res.picture.data.url,
- ProviderId: 'Facebook'
- }
- debugger;
- axios.post('http://localhost:60200/Api/Login/SocialmediaData', responseFacebook)
- .then((result) => {
- let responseJson = result;
- console.log(result.data.name);
- alert("data");
- sessionStorage.setItem("userData", JSON.stringify(result));
- this.props.history.push('/Dashboard')
- });
- };
- render() {
- const responseFacebook = (response) => {
- console.log(response);
- var res = response.profileObj;
- console.log(res);
- debugger;
- this.signup(response);
- }
- return (
- <div className="App">
- <div className="row">
- <div className="col-sm-12 btn btn-info">
- Login With Facebook Using ReactJS
- </div>
- </div>
- <div className="row">
- <div style={{ 'paddingTop': "20px" }} className="col-sm-12">
- <div className="col-sm-4"></div>
- <div className="col-sm-4">
- <FacebookLogin buttonStyle={{padding:"6px"}}
- appId="682908192243627"
- autoLoad={false}
- fields="name,email,picture"
- callback={responseFacebook}/>
- </div>
- <div className="col-sm-4"></div>
- </div>
- </div>
- </div>
- )
- }
- }
- export default Logintbyfacebook












Join the conversation! Your thoughts help the community grow.