Integrate Firebase Auth with Google login in your web app. Follow our step-by-step guide to set up Firebase, enable Google sign-in, and handle authentication state.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set Up Firebase Project
apiKey, authDomain, etc.
Step 2: Install & Set Up Firebase SDK
Using npm:
npm install firebase
Then, configure Firebase in your app:
<pre><code class="hljs">
import { initializeApp } from "firebase/app";
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID",
};
const app = initializeApp(firebaseConfig);
</code></pre>
Replace the placeholders with your actual configuration details from Firebase.
Step 3: Enable Google Sign-In
Step 4: Implement Google Authentication
Add Firebase Authentication to your app:
<pre><code class="hljs">
import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
const provider = new GoogleAuthProvider();
const auth = getAuth();
signInWithPopup(auth, provider)
.then((result) => {
// This gives you a Google Access Token. You can use it to access the Google API.
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
// The signed-in user info.
const user = result.user;
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.email;
// The AuthCredential type that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
});
</code></pre>
This script initiates a sign-in process with Google using a pop-up. Make sure you handle both success and error scenarios.
Step 5: Handle Authentication State
Monitor authentication state to update the UI or perform other actions when a user logs in or logs out:
<pre><code class="hljs">
import { getAuth, onAuthStateChanged } from "firebase/auth";
const auth = getAuth();
onAuthStateChanged(auth, (user) => {
if (user) {
// User is signed in
console.log("User signed in: ", user);
} else {
// User is signed out
console.log("No user is signed in");
}
});
</code></pre>
This section checks if a user is logged in. If so, it logs out information about the user.
Step 6: Log Out Functionality
Offer a sign-out option:
<pre><code class="hljs">
import { getAuth, signOut } from "firebase/auth";
const auth = getAuth();
signOut(auth).then(() => {
// Sign-out successful.
console.log("User signed out");
}).catch((error) => {
// An error happened.
console.error("Sign-out error", error);
});
</code></pre>
This function logs the user out and handles any potential errors.
By following these steps, you have successfully integrated Google Authentication using Firebase into your web app.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.