{"id":25631655,"url":"https://github.com/m2sd/workbox-plugin-firebase-auth","last_synced_at":"2025-07-02T12:35:03.633Z","repository":{"id":38778825,"uuid":"260778343","full_name":"m2sd/workbox-plugin-firebase-auth","owner":"m2sd","description":"Plugin to authorize workbox requests using firebase authentication","archived":false,"fork":false,"pushed_at":"2022-12-06T00:35:24.000Z","size":1310,"stargazers_count":7,"open_issues_count":11,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-19T22:53:17.187Z","etag":null,"topics":["firebase","firebase-auth","plugin","workbox","workbox-plugin"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/m2sd.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2020-05-02T21:29:27.000Z","updated_at":"2020-12-18T12:46:13.000Z","dependencies_parsed_at":"2023-01-24T07:46:06.886Z","dependency_job_id":null,"html_url":"https://github.com/m2sd/workbox-plugin-firebase-auth","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/m2sd/workbox-plugin-firebase-auth","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m2sd%2Fworkbox-plugin-firebase-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m2sd%2Fworkbox-plugin-firebase-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m2sd%2Fworkbox-plugin-firebase-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m2sd%2Fworkbox-plugin-firebase-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/m2sd","download_url":"https://codeload.github.com/m2sd/workbox-plugin-firebase-auth/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m2sd%2Fworkbox-plugin-firebase-auth/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262410335,"owners_count":23306840,"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":["firebase","firebase-auth","plugin","workbox","workbox-plugin"],"created_at":"2025-02-22T20:28:51.375Z","updated_at":"2025-07-02T12:35:03.601Z","avatar_url":"https://github.com/m2sd.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Workbox plugin for firebase auth\n\nThis is a simple plugin for workbox strategies which adds an `Authorization: Bearer` header with the return value from [`firebase.User.getIdToken(true)`](https://firebase.google.com/docs/reference/js/firebase.User#getidtoken) to the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) if a firebase User is authenticated (i.e. [`firebase.auth.Auth.onAuthStateChanged()`](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onauthstatechanged) returns a [`firebase.User`](https://firebase.google.com/docs/reference/js/firebase.User)).\n\n**CAUTION:** Be aware that request authorization happens before the response is passed to the caching strategy.  \nPlease plan accordingly (e.g. a cache first strategy might serve authorized content to non authorized users).\n\n## Usage\n\n### Module\n\nUse the module if you are building your service worker [using a bundler](https://developers.google.com/web/tools/workbox/guides/using-bundlers).\n\n1. Add the dependency:\n\n   ```sh\n   npm i workbox-firebase-auth // or yarn add workbox-firebase-auth\n   ```\n\n2. Import the initialization helper and use it to initialize firebase in the service worker.  \n   Import the plugin and use it for your strategies.\n\n   Example:\n\n   ```js\n   import { registerRoute } from 'workbox-routing/registerRoute.mjs';\n   import { NetworkFirst } from 'workbox-strategies/NetworkFirst.mjs';\n   import { initializeFirebase, Plugin as FirebaseAuthPlugin } from 'workbox-plugin-firebase-auth';\n\n   initializeFirebase({\n     config: { /* your firebase config */ },\n     services: ['messaging']\n   })\n\n   // `firebase` is now available in worker scope\n   firebase.auth()\n   firebase.messaging()\n\n   registerRoute(\n     /\\/api\\/.*/,\n     new NetworkFirst({\n       cacheName: 'authorizedApi',\n       plugins: [\n         new FirebaseAuthPlugin(),\n       ],\n     }),\n   );\n   ```\n\n### CDN\n\nIf you are using [workbox-sw](https://developers.google.com/web/tools/workbox/modules/workbox-sw) to import workbox, you can use the [unpkg CDN](https://unpkg.com/) to import the plugin.  \nIt will then be available under the global variable `WorkboxFirebaseAuth`.\n\nExample:\n\n```js\nimportScripts(\n  'https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js',\n  'https://unpkg.com/workbox-plugin-firebase-auth@1.1.0/lib/plugin.umd.js'\n)\n\nWorkboxFirebaseAuth.initializeFirebase({\n  config: { /* your firebase config */ },\n  services: ['messaging']\n})\n\n// `firebase` is now available in worker scope\nfirebase.auth()\nfirebase.messaging()\n\nworkbox.routing.registerRoute(\n  /\\/api\\/.*/,\n  new workbox.strategies.NetworkFirst({\n    cacheName: 'authorizedApi',\n    plugins: [\n      new WorkboxFirebaseAuth.Plugin(),\n    ],\n  }),\n)\n```\n\n## `initializeFirebase` options\n\nIf your service worker is hosted firebase hosting, associated with the firebase app you use to authorize users, you don't have to specify any options (the helper will load the firebase SDK from [reserved URLs](https://firebase.google.com/docs/hosting/reserved-urls)).  \nOtherwise the [`config`](#config) parameter is **REQUIRED**.\n\n### config\n\n**Type:** `object`  \n**Required:** If your service worker is NOT hosted on firebase hosting or if you use a different app to authorize users.\n\nThe [firebase config object](https://firebase.google.com/docs/web/setup?authuser=0#config-object) from the app that you use to authorize your users.\n\n### version\n\n**Type:** `string` (Firebase version)  \n**Default:** `7.19.1`\n\nThis option can be used to specify the firebase version to use.\n\n### services\n\n**Type:** `string[]`  \n**Default:** `[]`\n\nThis option can be use to load additional firebase services.  \nAvailable services are: (see: [Reserved URLs](https://firebase.google.com/docs/hosting/reserved-urls#libraries_hosting-urls))\n\n- `'auth'` (always included)\n- `'analytics'`\n- `'firestore'`\n- `'functions'`\n- `'messaging'`\n- `'storage'`\n- `'performance'`\n- `'database'`\n- `'config'`\n\n## `Plugin` options\n\n### awaitResponse\n\n**Type:** `boolean`  \n**Default:** `false`\n\nIf true the plugin will await the fetch to go through and check if the response has a 401 status before attaching the authorization and resending the request.\n\n\u003e **Note:** Please make sure your server responds to unauthorized requests with a 401 status code, so that the plugin can correctly identify authorization failures.\n\n### constraints\n\nThis key can be used to specify additional constraints on top of the route matcher.\n\n#### constraints.types\n\n**Type:** `string | string[]`  \n**Default:** `['*']`\n\nThis can be used to authorize only requests that accept certain types of responses (e.g. `application/json`)\n\n\u003e **Note:** This simply matches the entries from the `Accept` request header against the passed array/string.  \n\u003e Group matching is supported (e.g. `text/*` will match `text/html`, `text/plain` and `text/csv`)\n\n#### constraints.https\n\n**Type:** `boolean`  \n**Default:** `false`\n\nOnly allow requests to secure origins (`https://` or `localhost`) to be authorized.\n\n#### constraints.sameOrigin\n\n**Type:** `boolean`  \n**Default:** `true`\n\nOnly allow requests to the same origin as the service worker to be authorized.\n\n#### constraints.ignorePaths\n\n**Type:** `(string | RegExp)[]`  \n**Default:** `[]`\n\nPaths to ignore when authorizing requests.  \n\n\u003e **Note:** Checks against the pathname of the request (e.g. `/api/some-resource`)  \n\u003e If the argument is a `string` a request will be ignored if the pathname starts with that `string`.  \n\u003e If the argument is a `RegExp` a request will be ignored if the pathname matches the `RegExp`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm2sd%2Fworkbox-plugin-firebase-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fm2sd%2Fworkbox-plugin-firebase-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm2sd%2Fworkbox-plugin-firebase-auth/lists"}