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 🙏

© 2024 – Pkg Stats / Ryan Hefner

workbox-plugin-firebase-auth

v1.1.0

Published

Plugin to authorize workbox requests using firebase authentication

Downloads

12

Readme

Workbox plugin for firebase auth

This is a simple plugin for workbox strategies which adds an Authorization: Bearer header with the return value from firebase.User.getIdToken(true) to the Request if a firebase User is authenticated (i.e. firebase.auth.Auth.onAuthStateChanged() returns a firebase.User).

CAUTION: Be aware that request authorization happens before the response is passed to the caching strategy.
Please plan accordingly (e.g. a cache first strategy might serve authorized content to non authorized users).

Usage

Module

Use the module if you are building your service worker using a bundler.

  1. Add the dependency:

    npm i workbox-firebase-auth // or yarn add workbox-firebase-auth
  2. Import the initialization helper and use it to initialize firebase in the service worker.
    Import the plugin and use it for your strategies.

    Example:

    import { registerRoute } from 'workbox-routing/registerRoute.mjs';
    import { NetworkFirst } from 'workbox-strategies/NetworkFirst.mjs';
    import { initializeFirebase, Plugin as FirebaseAuthPlugin } from 'workbox-plugin-firebase-auth';
    
    initializeFirebase({
      config: { /* your firebase config */ },
      services: ['messaging']
    })
    
    // `firebase` is now available in worker scope
    firebase.auth()
    firebase.messaging()
    
    registerRoute(
      /\/api\/.*/,
      new NetworkFirst({
        cacheName: 'authorizedApi',
        plugins: [
          new FirebaseAuthPlugin(),
        ],
      }),
    );

CDN

If you are using workbox-sw to import workbox, you can use the unpkg CDN to import the plugin.
It will then be available under the global variable WorkboxFirebaseAuth.

Example:

importScripts(
  'https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js',
  'https://unpkg.com/[email protected]/lib/plugin.umd.js'
)

WorkboxFirebaseAuth.initializeFirebase({
  config: { /* your firebase config */ },
  services: ['messaging']
})

// `firebase` is now available in worker scope
firebase.auth()
firebase.messaging()

workbox.routing.registerRoute(
  /\/api\/.*/,
  new workbox.strategies.NetworkFirst({
    cacheName: 'authorizedApi',
    plugins: [
      new WorkboxFirebaseAuth.Plugin(),
    ],
  }),
)

initializeFirebase options

If 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).
Otherwise the config parameter is REQUIRED.

config

Type: object
Required: If your service worker is NOT hosted on firebase hosting or if you use a different app to authorize users.

The firebase config object from the app that you use to authorize your users.

version

Type: string (Firebase version)
Default: 7.19.1

This option can be used to specify the firebase version to use.

services

Type: string[]
Default: []

This option can be use to load additional firebase services.
Available services are: (see: Reserved URLs)

  • 'auth' (always included)
  • 'analytics'
  • 'firestore'
  • 'functions'
  • 'messaging'
  • 'storage'
  • 'performance'
  • 'database'
  • 'config'

Plugin options

awaitResponse

Type: boolean
Default: false

If 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.

Note: Please make sure your server responds to unauthorized requests with a 401 status code, so that the plugin can correctly identify authorization failures.

constraints

This key can be used to specify additional constraints on top of the route matcher.

constraints.types

Type: string | string[]
Default: ['*']

This can be used to authorize only requests that accept certain types of responses (e.g. application/json)

Note: This simply matches the entries from the Accept request header against the passed array/string.
Group matching is supported (e.g. text/* will match text/html, text/plain and text/csv)

constraints.https

Type: boolean
Default: false

Only allow requests to secure origins (https:// or localhost) to be authorized.

constraints.sameOrigin

Type: boolean
Default: true

Only allow requests to the same origin as the service worker to be authorized.

constraints.ignorePaths

Type: (string | RegExp)[]
Default: []

Paths to ignore when authorizing requests.

Note: Checks against the pathname of the request (e.g. /api/some-resource)
If the argument is a string a request will be ignored if the pathname starts with that string.
If the argument is a RegExp a request will be ignored if the pathname matches the RegExp.