Kritim Yantra
Jun 22, 2025
Ever tried building a login system from scratch? It’s like baking a cake 🎂—except you have to grow the wheat, milk the cow, and refine the sugar first! 😅
Luckily, Firebase does all the heavy lifting for us, and React makes it smooth and interactive. By the end of this guide, you’ll have a secure, production-ready authentication system with:
✅ Email/Password Login & Signup
✅ Google & GitHub OAuth (because who remembers passwords?)
✅ Protected Routes (keep unauthorized users out)
✅ Real-time User State Management
Let’s dive in!
Before we start, make sure you have:
npx create-react-app firebase-auth
) Firebase is like a backend superhero 🦸—it handles authentication, databases, and more without writing server code.
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_SENDER_ID",
appId: "YOUR_APP_ID"
};
Run:
npm install firebase @react-firebase/auth
Create a file firebase.js
:
import { initializeApp } from "firebase/app";
import { getAuth, GoogleAuthProvider } from "firebase/auth";
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_SENDER_ID",
appId: "YOUR_APP_ID"
};
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
export const googleProvider = new GoogleAuthProvider();
Now, Firebase is ready to use!
import { createUserWithEmailAndPassword } from "firebase/auth";
import { auth } from "./firebase";
const signUp = async (email, password) => {
try {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
console.log("User created:", userCredential.user);
} catch (error) {
console.error("Error signing up:", error.message);
}
};
import { signInWithEmailAndPassword } from "firebase/auth";
const login = async (email, password) => {
try {
const userCredential = await signInWithEmailAndPassword(auth, email, password);
console.log("User logged in:", userCredential.user);
} catch (error) {
console.error("Error logging in:", error.message);
}
};
import { signInWithPopup } from "firebase/auth";
const signInWithGoogle = async () => {
try {
const result = await signInWithPopup(auth, googleProvider);
console.log("Google user:", result.user);
} catch (error) {
console.error("Google auth error:", error.message);
}
};
Want to restrict pages to logged-in users only? Use React Router + Firebase Auth:
import { onAuthStateChanged } from "firebase/auth";
const PrivateRoute = ({ children }) => {
const [user, setUser] = useState(null);
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (currentUser) => {
setUser(currentUser);
});
return () => unsubscribe();
}, []);
return user ? children : <Navigate to="/login" />;
};
Now wrap protected routes:
<Route path="/dashboard" element={<PrivateRoute><Dashboard /></PrivateRoute>} />
Use React Context or Redux to manage the user globally. Here’s a simple version:
import { createContext, useEffect, useState } from "react";
import { auth } from "./firebase";
export const AuthContext = createContext();
export const AuthProvider = ({ children }) => {
const [currentUser, setCurrentUser] = useState(null);
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (user) => {
setCurrentUser(user);
});
return unsubscribe;
}, []);
return (
<AuthContext.Provider value={{ currentUser }}>
{children}
</AuthContext.Provider>
);
};
Wrap your app with it:
<AuthProvider>
<App />
</AuthProvider>
Now, any component can access currentUser
!
You now have a full authentication system with:
✔ Firebase Auth makes authentication effortless
✔ React + Firebase is a powerful combo for full-stack apps
✔ Protected routes ensure only logged-in users access certain pages
✔ User state management keeps your app in sync
Got stuck? Drop a comment below, and I’ll help you out! 👇
#HappyCoding 💻🔥
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google