Skip to content
Go back

Can I Use Firebase Only for Authentication?

Updated:
Edit page

Introduction

Hey there, fellow developer! Ever found yourself scratching your head, wondering if you can cherry-pick just one delicious piece from the Firebase pie? Specifically, can you use Firebase Authentication without diving headfirst into Firestore, Realtime Database, or all the other amazing services? The short answer is a resounding yes!

As someone who’s built a fair share of apps, I totally get the desire for flexibility. Sometimes you have an existing backend, a preferred database, or just want to keep things lean. Firebase Authentication is a powerful, standalone solution for managing user logins, and in this post, I’ll show you exactly why and how you can leverage it on its own.


Why Go “Auth-Only” with Firebase?

Before we jump into the “how,” let’s talk about the “why.” You might consider using Firebase Authentication in isolation for several compelling reasons:


Getting Started: Setting Up Your Firebase Project

First things first, you’ll need a Firebase project. Don’t worry, it’s super straightforward.

Create a New Firebase Project:

Go to the Firebase Console. Click “Add project” and follow the prompts. Give it a meaningful name!

Enable Authentication:

Once your project is created, navigate to the “Authentication” section in the left-hand menu. Click on the “Sign-in method” tab. Enable the authentication providers you plan to use (e.g., Email/Password, Google). You’ll typically configure basic settings for each.


Integrating Firebase Authentication into Your Application (Code Samples!)

Now, let’s get down to the code. The beauty of Firebase Auth is its well-documented SDKs for various platforms. I’ll show examples for a web application using JavaScript, but the principles apply across mobile and other environments.

Step 1: Install the Firebase SDK

For a web project, you’d typically install it via npm or yarn:

npm install firebase
# or
yarn add firebase

Step 2: Initialize Firebase in Your App

You’ll need your Firebase project’s configuration. You can find this in your Firebase Console under “Project settings” (the gear icon) -> “Your apps”.

// firebaseConfig.js
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";

// Your web app's Firebase configuration
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"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);

Step 3: User Registration (Email & Password Example)

Here’s how you might implement a user registration function. Notice we’re just using createUserWithEmailAndPassword from the firebase/auth module.

// authService.js
import { createUserWithEmailAndPassword } from "firebase/auth";
import { auth } from './firebaseConfig';

export const registerUser = async (email, password) => {
  try {
    const userCredential = await createUserWithEmailAndPassword(auth, email, password);
    const user = userCredential.user;
    console.log("User registered successfully!", user.uid);
    // You can now send this user.uid to your custom backend to store
    // any additional user profile data in your preferred database.
    return user;
  } catch (error) {
    const errorCode = error.code;
    const errorMessage = error.message;
    console.error("Error registering user:", errorCode, errorMessage);
    throw error; // Re-throw to handle in UI
  }
};

Step 4: User Login (Email & Password Example)

Similarly, logging in is a breeze:

// authService.js (continued)
import { signInWithEmailAndPassword } from "firebase/auth";

// ... (other imports)

export const loginUser = async (email, password) => {
  try {
    const userCredential = await signInWithEmailAndPassword(auth, email, password);
    const user = userCredential.user;
    console.log("User logged in successfully!", user.uid);
    return user;
  } catch (error) {
    const errorCode = error.code;
    const errorMessage = error.message;
    console.error("Error logging in user:", errorCode, errorMessage);
    throw error;
  }
};

Step 5: Handling Authentication State

Firebase Auth provides a convenient way to listen for changes in the user’s login state:

// authService.js (continued)
import { onAuthStateChanged } from "firebase/auth";

// ... (other imports)

export const observeAuthState = (callback) => {
  onAuthStateChanged(auth, (user) => {
    if (user) {
      // User is signed in
      console.log("User is signed in:", user.uid);
      // You can get the ID token to authenticate with your custom backend
      user.getIdToken().then((idToken) => {
        console.log("Firebase ID Token:", idToken);
        // Send this ID token to your custom backend for verification!
      });
      callback(user);
    } else {
      // User is signed out
      console.log("User is signed out.");
      callback(null);
    }
  });
};

// Example usage in your app:
// observeAuthState((user) => {
//   if (user) {
//     // Update UI for logged-in user
//   } else {
//     // Update UI for logged-out user, redirect to login page
//   }
// });

Important: When using Firebase Auth with a separate backend, you’ll want to verify the Firebase ID token on your server. This token is a JSON Web Token (JWT) signed by Google, proving the user’s identity. Your backend can then extract the user’s UID and other claims to authorize requests.


What About Pricing?

One common concern when using Firebase is cost. Good news! Firebase Authentication has a very generous free tier (Spark plan).

You can check the detailed Firebase pricing page for the latest information, but rest assured, using just Authentication is highly cost-effective.


Conclusion: Your Authentication, Your Way!

So, there you have it! You absolutely can use Firebase solely for authentication. It’s a fantastic solution for developers who need robust, scalable, and easy-to-implement user management without needing the full suite of Firebase backend services.

Whether you’re building a new app from scratch or integrating authentication into an existing system, Firebase Auth offers the flexibility to get your users signed in securely and efficiently. Don’t be afraid to mix and match tools to build the perfect stack for your project. Happy coding!


Edit page
Share this post on:

Previous Post
Does Firebase Auth Use JWT? Unpacking Authentication for Your App
Next Post
Is Firebase Auth Truly Free? A Beginner's Guide to Comparing Authentication Service Costs