Skip to content

Latest commit

 

History

History
38 lines (32 loc) · 1.01 KB

File metadata and controls

38 lines (32 loc) · 1.01 KB

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({});
  });
});