0
0
FirebaseHow-ToBeginner · 4 min read

How to Use Firebase Auth Emulator for Local Testing

To use the Firebase Auth Emulator, start it with firebase emulators:start --only auth and configure your app to connect to it using connectAuthEmulator(auth, 'http://localhost:9099'). This lets you test authentication locally without touching your live Firebase project.
📐

Syntax

The Firebase Auth Emulator is started using the Firebase CLI command firebase emulators:start --only auth. In your app code, you connect to the emulator by calling connectAuthEmulator(auth, 'http://localhost:9099'), where auth is your Firebase Auth instance and http://localhost:9099 is the default emulator URL.

This setup redirects authentication requests to the local emulator instead of the live Firebase service.

bash and javascript
firebase emulators:start --only auth

import { getAuth, connectAuthEmulator } from 'firebase/auth';

const auth = getAuth();
connectAuthEmulator(auth, 'http://localhost:9099');
💻

Example

This example shows how to start the Firebase Auth Emulator and connect a simple web app to it for local testing of sign-in.

javascript
/* Start the emulator in your terminal */
firebase emulators:start --only auth

/* In your web app code */
import { initializeApp } from 'firebase/app';
import { getAuth, signInWithEmailAndPassword, connectAuthEmulator } from 'firebase/auth';

const firebaseConfig = {
  apiKey: 'fake-api-key',
  authDomain: 'localhost',
  projectId: 'demo-project',
};

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);

// Connect to the local Auth emulator
connectAuthEmulator(auth, 'http://localhost:9099');

// Sign in a test user
signInWithEmailAndPassword(auth, 'test@example.com', 'password123')
  .then(userCredential => {
    console.log('Signed in user:', userCredential.user.email);
  })
  .catch(error => {
    console.error('Error signing in:', error.message);
  });
Output
Signed in user: test@example.com
⚠️

Common Pitfalls

  • Not starting the emulator before running your app causes connection errors.
  • Forgetting to call connectAuthEmulator means your app talks to live Firebase, risking real data changes.
  • Using the wrong emulator URL or port (default is http://localhost:9099) will fail connection.
  • Running emulator commands without initializing Firebase project config (firebase init) can cause errors.
javascript
/* Wrong: Missing connectAuthEmulator call */
const auth = getAuth();
// No connectAuthEmulator call here

/* Right: Connect to emulator */
connectAuthEmulator(auth, 'http://localhost:9099');
📊

Quick Reference

StepCommand / CodeDescription
1firebase emulators:start --only authStart the Firebase Auth Emulator locally
2import { getAuth, connectAuthEmulator } from 'firebase/auth';Import Firebase Auth functions
3const auth = getAuth();Initialize Firebase Auth instance
4connectAuthEmulator(auth, 'http://localhost:9099');Connect app to local Auth emulator
5Use auth methods (e.g., signInWithEmailAndPassword) as usualTest authentication locally without affecting live data

Key Takeaways

Always start the Firebase Auth Emulator with the CLI before running your app.
Use connectAuthEmulator() in your app to redirect auth calls to the emulator.
The default emulator URL is http://localhost:9099 unless configured otherwise.
Testing with the emulator prevents changes to your live Firebase project.
Ensure your Firebase project is initialized with firebase init before using emulators.