npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@my-f-startup/firebase-auth-express

v0.1.0

Published

Express middleware and guards for Firebase Authentication.

Readme

firebase-auth-express

Zero-trust Firebase Authentication middleware for Express.js.

Express middleware and guards for Firebase Authentication. Validates ID tokens, exposes the authenticated user context to handlers, and enforces role-based access control. Includes unit, integration (in-memory), and E2E tests with Firebase Auth emulator via Testcontainers.


Why this exists

Firebase Authentication provides secure identity tokens, but most Express applications end up repeating the same boilerplate in every route:

  • Extracting Authorization: Bearer <token>
  • Calling verifyIdToken
  • Handling missing or invalid tokens
  • Propagating uid and token claims
  • Enforcing role-based access
  • Mocking authentication in tests
  • Integrating with the Firebase Auth emulator

This package centralizes those concerns into a single, composable, and testable authentication layer, so your handlers can focus purely on business logic.


Features

  • Zero-trust authentication – Validates Firebase ID tokens on every request
  • Typed identity context – Populates req.auth with uid and token claims
  • Role-based authorizationrequireRole enforces custom-claim roles
  • Composable guards – Clear separation between authentication and authorization
  • Emulator-first testing – Unit, integration, and E2E tests using Firebase Auth emulator

Installation

npm install @my-f-startup/firebase-auth-express

Quick Start (dev)

import express from "express";
import admin from "firebase-admin";
import {
  firebaseAuthMiddleware,
  requireAuth,
  requireRole,
} from "@my-f-startup/firebase-auth-express";

admin.initializeApp({ projectId: "demo-project" });

const app = express();
app.use(express.json());

// Global authentication middleware
app.use(firebaseAuthMiddleware());

app.get(
  "/me",
  requireAuth((req, res) => {
    res.json({ uid: req.auth!.uid });
  })
);

app.get(
  "/admin",
  requireRole("admin", (req, res) => {
    res.json({ uid: req.auth!.uid });
  })
);

app.listen(3000, () => console.log("listening on http://localhost:3000"));

Mental model

This package separates authentication concerns into two layers:

1. Middleware

  • firebaseAuthMiddleware
  • Runs once per request
  • Extracts and validates the Firebase ID token
  • Populates req.auth

2. Guards

  • requireAuth
  • requireRole
  • Applied per-route
  • Enforce access rules before executing handlers

This model keeps authentication logic centralized and routes clean.


Using the Firebase Auth Emulator

When using the Firebase Auth emulator locally:

export FIREBASE_AUTH_EMULATOR_HOST=localhost:9099
export PROJECT_ID=demo-project

The firebase-admin SDK automatically routes authentication calls to the emulator when FIREBASE_AUTH_EMULATOR_HOST is set.

Emulator image for E2E tests

The E2E suite uses the Docker image myfstartup/firebase-emulator-suite:15 via Testcontainers. The container expects PROJECT_ID and supports mounting firebase.json and .firebaserc into /app for configuration.


API

firebaseAuthMiddleware(options?)

Factory that returns an Express middleware. It:

  • Requires Authorization: Bearer <token>
  • Calls admin.auth().verifyIdToken(token)
  • Sets req.auth = { uid, token }
  • Returns 401 on missing or invalid token
  • Returns 500 if the auth infrastructure is not initialized

You can inject a custom auth client for tests:

firebaseAuthMiddleware({
  authClient: {
    verifyIdToken: async (token) => ({ uid: "user-1" } as any),
  },
});

requireAuth(handler)

Ensures that the request is authenticated before executing the handler.

  • Returns 401 if req.auth is missing

requireRole(role | roles[], handler)

Ensures the authenticated user has the required role(s), as defined in Firebase custom claims.

  • Returns 401 if req.auth is missing
  • Returns 403 if required role(s) are not present in req.auth.token.roles

Security model

  • Stateless authentication
  • No sessions
  • No token caching
  • Every request validates its Firebase ID token
  • Authorization is based exclusively on verified token claims

This design favors correctness and security over performance shortcuts.


Testing philosophy

This package treats the Firebase Auth emulator as a first-class dependency.

  • Unit tests – Pure logic
  • Integration tests – In-memory auth clients
  • E2E tests – Real Firebase Auth emulator via Testcontainers

This ensures production-like behavior without external dependencies.


Tests

  • Unit: npm run test:unit
  • Integration (in-memory): npm run test:int
  • E2E (Auth emulator): npm run test:e2e
  • All tests: npm test

Build & Runtime

  • Dev: npm start (tsx)
  • Build JS: npm run build (outputs dist/)

Compatibility

  • Node.js: >= 20
  • Firebase Admin SDK: firebase-admin

Non-goals

  • User management
  • Session handling
  • Token refresh
  • OAuth flows

This package focuses strictly on request authentication and authorization.