Implementing authentication from scratch can be time-consuming, especially when users expect to sign in with a single click. OAuth 2.0 authentication solves this problem by allowing users to log in using their existing accounts from providers like Facebook.
In this tutorial, you'll learn how to integrate Facebook Sign-In into a React application using Firebase Authentication. Firebase securely handles the OAuth flow while React manages your application's UI.
By the end of this guide, users will be able to sign in and sign out using their Facebook account with just a few lines of code.
π Prerequisites
Before getting started, make sure you have:
- React (Vite) project
- Firebase project
- Firebase Authentication enabled
- Meta Developer account
- Basic understanding of React Hooks
Install Firebase:
npm install firebase
Step 1: Create a Firebase Project
Navigate to the Firebase Console and create a new project.
Once your project is created:
- Add a new Web App
- Copy your Firebase configuration
- Install the Firebase SDK
Step 2: Enable Facebook Authentication
Inside Firebase Console:
Authentication
β
Sign-in Method
β
Facebook
Enable Facebook Authentication.
You'll need:
- Facebook App ID
- Facebook App Secret
These credentials are obtained from your Meta Developer application.
Step 3: Create a Meta (Facebook) App
Visit the Meta Developers Dashboard.
Create a new application and add the Facebook Login product.
After creating the application you'll receive:
- App ID
- App Secret
Copy both values into Firebase Authentication settings.
Finally, add the OAuth Redirect URI provided by Firebase into your Facebook application's OAuth settings.
Step 4: Configure Firebase
Create a firebase.js file.
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
const firebaseConfig = {
apiKey: "...",
authDomain: "...",
projectId: "...",
appId: "...",
};
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
Step 5: Create the Facebook Provider
import { FacebookAuthProvider } from "firebase/auth";
export const facebookProvider = new FacebookAuthProvider();
If your application requires additional permissions:
facebookProvider.addScope("email");
Step 6: Implement Facebook Login
import {
FacebookAuthProvider,
signInWithPopup,
} from "firebase/auth";
import { auth } from "./firebase";
const provider = new FacebookAuthProvider();
const loginWithFacebook = async () => {
try {
const result = await signInWithPopup(auth, provider);
console.log(result.user);
} catch (error) {
console.log(error);
}
};
Step 7: Create the Login Button
<button onClick={loginWithFacebook}>
Continue with Facebook
</button>
When users click the button:
- Facebook popup opens
- User grants permission
- Firebase authenticates the user
- User information becomes available
Step 8: Access User Information
const user = result.user;
console.log(user.displayName);
console.log(user.email);
console.log(user.photoURL);
Typical user information includes:
- UID
- Name
- Profile Picture
- Access Token
Step 9: Implement Logout
import { signOut } from "firebase/auth";
const logout = async () => {
await signOut(auth);
};
<button onClick={logout}>
Logout
</button>
Step 10: Observe Authentication State
Instead of manually checking login status, Firebase provides a listener.
import { onAuthStateChanged } from "firebase/auth";
onAuthStateChanged(auth, (user) => {
if (user) {
console.log("Logged in");
} else {
console.log("Logged out");
}
});
This keeps your React UI synchronized with the authentication state.
π Project Structure
src/
β
βββ firebase.js
βββ App.jsx
βββ Login.jsx
βββ Navbar.jsx
βββ main.jsx
π¨ Common Errors
Popup Closed
The user closed the authentication popup before completing login.
Unauthorized Domain
Add your application's domain inside:
Firebase Authentication
β
Authorized Domains
Invalid OAuth Redirect
Ensure the redirect URI from Firebase exactly matches the one configured inside your Meta Developer application.
App Not Live
If your Facebook application is still in Development Mode, only authorized test users can authenticate.
β Best Practices
- Never expose sensitive credentials outside Firebase configuration.
- Use
onAuthStateChanged()to manage authentication state. - Handle authentication errors gracefully.
- Protect private routes after successful login.
- Store additional user profile data in Firestore or your own backend.
π Conclusion
Facebook Sign-In provides a fast and familiar authentication experience for users. By combining React with Firebase Authentication, you can integrate secure OAuth 2.0 login without implementing the entire authentication flow yourself.
In this tutorial, you learned how to:
- Configure Firebase Authentication
- Create a Meta Developer application
- Enable Facebook Login
- Authenticate users using
signInWithPopup - Read authenticated user information
- Sign users out
- Track authentication state
With only a few Firebase APIs, you can add production-ready Facebook authentication to your React applications.
π₯ Watch the Complete Video Tutorial (Part 16)
Prefer learning by building?
Follow the complete hands-on implementation in Part 16 of the React Authentication & Authorization Playlist, where we build Facebook OAuth Sign-In from scratch using React and Firebase Authentication.
πΊ Watch here
https://www.youtube.com/watch?v=z1SpI42MlzU&list=PL_02r0p8Ku_5-h4teExCf6egkktSQblC4&index=16
π Follow the Complete React Authentication & Authorization Playlist
This tutorial is one part of a complete series covering modern authentication in React.
Throughout the playlist, you'll learn how to build secure authentication systems using:
- Firebase Authentication
- JWT Authentication
- OAuth Providers
- Protected Routes
- Role-based Authorization
- Password Reset
- Email Verification
- Authentication Best Practices
π¬ Start the playlist here
https://www.youtube.com/watch?v=gbjqaiwjTZ8&list=PL_02r0p8Ku_5-h4teExCf6egkktSQblC4&index=1
If you found this article helpful, consider:
- β€οΈ Leaving a reaction
- π¬ Sharing your thoughts in the comments
- π Saving it for future reference
- π Following me on Dev.to for more React and Firebase tutorials
Happy coding! π
Top comments (0)