React Facebook Login Component
raw JSON →This package, `react-facebook-login`, provides a React component designed to simplify integrating Facebook authentication into web applications. As of its latest version, 4.1.1, the component allows developers to add a styled Facebook login button or implement a custom UI using a render prop, handling the underlying Facebook SDK interactions and authentication flow. It supports fetching user profiles (name, email, picture) and requesting custom permissions (scopes). The package offers a straightforward way to integrate Facebook login without direct manipulation of the Facebook JavaScript SDK. Given its last significant update was in the 2019/2020 timeframe, its release cadence is effectively dormant, and developers should be aware of potential incompatibilities with newer React versions or changes in Facebook's API. Its primary differentiator was its simplicity and ability to provide a quick Facebook login integration out-of-the-box or with custom rendering.
Common errors
error Facebook SDK not loaded ↓
appId prop on the FacebookLogin component. Check the browser's developer console for any network errors related to connect.facebook.net or other SDK resources. error "window.FB is undefined" or "FB is not defined" ↓
error Error: Given URL is not allowed by the Application configuration: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the Mobile Site URL in your App's settings. ↓
http://localhost:3000, https://your-domain.com) where your app runs, including the protocol (HTTP/HTTPS) and port number. error Auth Dialog response: An error occurred. Please try again later. ↓
appId, the list of requested scope permissions, and confirm that your Facebook App is 'Live' and properly configured in the Facebook Developer Console. If all settings appear correct, try again after some time, as it might be a transient issue with Facebook's services. Warnings
breaking The Facebook Graph API and its JavaScript SDK undergo frequent updates. This package (last updated v4.1.1, around 2019/2020) likely relies on an older version of the Facebook SDK and API. This can cause login failures, unexpected behavior, or security vulnerabilities due to incompatibilities with current Facebook platform requirements. ↓
breaking This `react-facebook-login` package is no longer actively maintained. There have been no new features, bug fixes, or security patches released since its last update in the 2019/2020 timeframe. Using abandoned software can introduce significant security vulnerabilities, compatibility issues with newer React versions or browser standards, and a lack of support for critical bugs. ↓
gotcha The `appId` prop is absolutely required for the component to function correctly. Omitting or providing an invalid App ID will prevent the Facebook SDK from initializing properly, leading to login failures and errors in the browser console. ↓
gotcha Using `autoLoad={true}` can lead to an infinite login loop or unexpected token refreshes, especially in single-page applications where the component might be unmounted and remounted frequently. This can result in a poor user experience or rate-limiting issues. ↓
gotcha The example code for custom icons, such as `<TiSocialFacebookCircular />`, uses an outdated import path for `react-icons` (e.g., `'react-icons/lib/ti/social-facebook-circular'`). Modern versions of `react-icons` use a different import structure. ↓
Install
npm install react-facebook-login yarn add react-facebook-login pnpm add react-facebook-login Imports
- FacebookLogin wrong
const FacebookLogin = require('react-facebook-login')correctimport FacebookLogin from 'react-facebook-login' - FacebookLogin (render prop) wrong
import { FacebookLogin } from 'react-facebook-login/dist/facebook-login-render-props'correctimport FacebookLogin from 'react-facebook-login/dist/facebook-login-render-props' - responseFacebook (callback)
const responseFacebook = (response) => { /* handle response */ }
Quickstart
import React from 'react';
import ReactDOM from 'react-dom';
import FacebookLogin from 'react-facebook-login';
const componentClicked = () => {
console.log('Facebook login button clicked!');
};
const responseFacebook = (response) => {
console.log('Facebook response received:', response);
if (response && !response.status) {
console.log('User logged in:', response.name, response.email);
// Typically, you would send response.accessToken to your backend for server-side verification
// Example:
// fetch('/api/auth/facebook', {
// method: 'POST',
// headers: { 'Content-Type': 'application/json' },
// body: JSON.stringify({ accessToken: response.accessToken, userID: response.userID })
// });
} else {
console.log('Login failed or cancelled:', response);
}
};
ReactDOM.render(
<FacebookLogin
appId="YOUR_FACEBOOK_APP_ID" // <-- IMPORTANT: Replace with your actual App ID
autoLoad={false} // Set to true to attempt automatic login on page load. Use with caution.
fields="name,email,picture" // Requested user data fields
scope="public_profile,email" // Requested permissions
onClick={componentClicked} // Optional: callback when the button is clicked
callback={responseFacebook} // Required: callback with login response
cssClass="my-custom-facebook-button" // Optional: add custom CSS class
icon="fa-facebook" // Optional: use Font Awesome icon. Ensure Font Awesome is loaded.
textButton="Login with Facebook" // Optional: custom button text
/>,
document.getElementById('root') // Ensure an element with id="root" exists in your HTML
);