{"id":21451660,"url":"https://github.com/widavies/fireflare","last_synced_at":"2025-07-14T22:30:38.668Z","repository":{"id":37449399,"uuid":"353479709","full_name":"widavies/fireflare","owner":"widavies","description":"Firebase authentication for Cloudflare workers","archived":false,"fork":false,"pushed_at":"2022-10-07T16:52:36.000Z","size":28,"stargazers_count":16,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-15T11:35:29.467Z","etag":null,"topics":["authentication","cloudflare","cloudflare-workers","firebase","firebase-auth"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/widavies.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-31T20:18:55.000Z","updated_at":"2024-04-20T16:22:32.000Z","dependencies_parsed_at":"2022-08-19T19:40:07.376Z","dependency_job_id":null,"html_url":"https://github.com/widavies/fireflare","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/widavies%2Ffireflare","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/widavies%2Ffireflare/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/widavies%2Ffireflare/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/widavies%2Ffireflare/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/widavies","download_url":"https://codeload.github.com/widavies/fireflare/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226000354,"owners_count":17557747,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authentication","cloudflare","cloudflare-workers","firebase","firebase-auth"],"created_at":"2024-11-23T04:24:35.100Z","updated_at":"2024-11-23T04:24:35.578Z","avatar_url":"https://github.com/widavies.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FireFlare\nFirebase authentication for Cloudflare workers with no dependencies. Google public keys are cached with Workers KV to speed up authentication.\n\n# Installation\n`npm i fireflare`\n\nhttps://www.npmjs.com/package/fireflare\n# Usage\n```typescript\nimport {auth} from \"fireflare\"\n\n// Taken from https://github.com/cloudflare/worker-template-router\nasync function handleRequest(request: Request) {\n  const r = new Router()\n\n  r.get('/', async (request) =\u003e {\n    if(!(await auth('projectId', KV_NAMESPACE, request.headers.get('Authorization')?.replace(\"Bearer \", \"\") ?? null))) {\n      // Not authenticated\n      return new Response(\"Unauthorized\");\n    }\n\n    return new Response(\"Authorized\");\n  }) // return a default message for the root route\n\n  const resp = await r.route(request)\n  return resp\n}\n```\n# Additional/custom claims validation\n```typescript\nif (!(await auth('projectId', env.KV_NAMESPACE, request.headers.get('Authorization')?.replace(\"Bearer \", \"\") ?? null, [\n  // Use a helper claims check function\n  Equals('custom-claim', 'expected-value'),\n  InPast('issued'),\n  InFuture('exp'),\n  NotEmpty('sub'),\n  // Validate claims with custom implementation\n  (claims) =\u003e {\n    const value = claims['custom-claim'];\n    // Return false to reject\n    return (typeof value === 'string' || value instanceof String) \u0026\u0026 value === \"custom-value\";\n  }\n]))) {\n  // Not authenticated\n  return new Response(\"Unauthorized\");\n}\n```\n\n# Example authentication using [itty-router](https://github.com/kwhitley/itty-router)\n```typescript\n\n// Auth middleware\nconst requireUser = async (request: Request, env: Env) =\u003e {\n  const token = request.headers.get('Authorization')?.replace('Bearer ', '');\n\n  return auth(env.FIREBASE_PROJECT_ID, env.KV_NAMESPACE, token ?? null).then((success) =\u003e {\n    if (!success) {\n      return new Response(\"Not authenticated\", { status: 401 });\n    }\n  }).catch((err) =\u003e {\n    return new Response(\"Not authenticated\", { status: 401 });\n  });\n};\n\nrouter.get('/hello-auth', requireUser, async (request, env) =\u003e {\n  return new Response(\"Authenticated\", { status: 200 });\n});\n\n```\n\n# Bonus: Full example (including CORS)\n```typescript\nexport interface Env {\n  FIREBASE_PROJECT_ID: string,\n\n  KV_NAMESPACE: KVNamespace;\n}\n\nconst CorsHeaders = {\n  \"Access-Control-Allow-Origin\": \"https://www.example.com\",\n  \"Access-Control-Allow-Methods\": \"GET,HEAD,POST,OPTIONS,DELETE\",\n  \"Access-Control-Max-Age\": \"86400\",\n}\n\nexport default {\n  async fetch (request: Request, env: Env, context: ExecutionContext) {\n    if (request.method === \"OPTIONS\") {\n      return handleOptions(request)\n    } else {\n      const response = await router.handle(request, env);\n\n      if (response instanceof Response) {\n        for (const [key, value] of Object.entries(CorsHeaders)) {\n          response.headers.set(key, value);\n        }\n      }\n\n      return response;\n    }\n  }\n};\n\nconst router = Router();\n\nconst requireUser = async (request: Request, env: Env) =\u003e {\n  const token = request.headers.get('Authorization')?.replace('Bearer ', '');\n\n  return auth(env.FIREBASE_PROJECT_ID, env.KV_NAMESPACE, token ?? null).then((success) =\u003e {\n    if (!success) {\n      return new Response(\"Not authenticated\", { status: 401 });\n    }\n  }).catch((err) =\u003e {\n    return new Response(\"Not authenticated\", { status: 401 });\n  });\n};\n\nrouter.get('/hello-auth', requireUser, async (request: IttyRequest, env: Env) =\u003e {\n  return new Response(\"Authenticated\", { status: 200 });\n});\n\n\nrouter.all('*', () =\u003e new Response('Not Found.', { status: 404 }));\n\n// Cors header stuff\nfunction handleOptions (request: Request) {\n  // Make sure the necessary headers are present\n  // for this to be a valid pre-flight request\n  let headers = request.headers\n  if (\n    headers.get(\"Origin\") !== null \u0026\u0026\n    headers.get(\"Access-Control-Request-Method\") !== null \u0026\u0026\n    headers.get(\"Access-Control-Request-Headers\") !== null\n  ) {\n    // Handle CORS pre-flight request.\n    // If you want to check or reject the requested method + headers\n    // you can do that here.\n    let respHeaders = {\n      ...CorsHeaders,\n      // Allow all future content Request headers to go back to browser\n      // such as Authorization (Bearer) or X-Client-Name-Version\n      \"Access-Control-Allow-Headers\": request.headers.get(\"Access-Control-Request-Headers\") as string,\n    }\n    return new Response(null, {\n      headers: respHeaders,\n    })\n  } else {\n    // Handle standard OPTIONS request.\n    // If you want to allow other HTTP Methods, you can do that here.\n    return new Response(null, {\n      headers: {\n        Allow: \"GET, HEAD, POST, OPTIONS, DELETE\",\n      },\n    })\n  }\n}\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwidavies%2Ffireflare","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwidavies%2Ffireflare","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwidavies%2Ffireflare/lists"}