Skip to content

frankleng/cloudflare-firebase-jwt-verifier

Repository files navigation

cloudflare-firebase-jwt-verifier

A lightweight JWT verifier for Firebase Auth running on Cloudflare Workers. This lib fetches, caches keys from Firebase, and verifies the JWT token.

Why

Cloudflare Workers runtime doesn't support Node.js core modules, which means we cannot use common libs like jsonwebtoken.

Install

npm i --save cloudflare-firebase-jwt-verifier
yarn add cloudflare-firebase-jwt-verifier

Usage

import { getVerifier, JwtInvalidError } from 'cloudflare-firebase-jwt-verifier';

const { verify } = getVerifier(firebaseProjectId);

export async function verifyAuth(request: Request) {
  const header = request.headers.get('Authorization');
  if (!header) {
    throw new JwtInvalidError();
  }
  return await verify(header);
}

addEventListener('fetch', (event) => {
  event.passThroughOnException();
  event.respondWith(async (event) => {
    const auth = await verifyAuth(request);
    const userId = auth?.payload.sub;
    return new Response({});
  });
});

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published