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

firebase-device-store

v0.2.0

Published

Automatically store Device and FCM token information for Firebase Auth users in Cloud Firestore.

Downloads

2

Readme

Firebase Device Store

Automatically store Device and FCM Token information for Firebase Auth Users in Cloud Firestore.

npm version npm downloads

This library is a proof of concept, and very much a work in progress.

Installation

Firebase Device Store requires Firebase v5.0.0 or later.

npm install --save firebase-device-store

Setup

The following Firebase libraries need to be enabled in your application:

import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
import 'firebase/messaging';

Example usage

import DeviceStore from 'firebase-device-store';

const deviceStore = DeviceStore(firebase.app(), 'user-devices');

Documentation

Firebase Device Store automatically stores device and FCM information for Firebase Auth users in Cloud Firestore.

When creating a device store, it will:

  1. Request appropriate Firebase Messaging permissions, if they have not already been granted
  2. Subscribe to Firebase Auth and listen to changes in authentication state
  3. Subscribe to Firebase Messaging and listen to changes in the FCM token
  4. Automatically store device and FCM token information in the Cloud Firestore collection you specify

Data Model

A Document is created in the Cloud Firestore collection for each logged in user:

/user-devices
  - userId1: {},
  - userId2: {},

The structure of this Document is as follows:

{
  devices: Device[],
  userId: string,
}

A Device object contains the following:

{
  deviceId: string, // The browser's user agent
  fcmToken: string, // The FCM token
  name: string,     // The name of the browser
  os: string,       // The OS of the device
  type: 'web'
}

API

DeviceStore(app, collectionPath)

Parameters:

  • app: firebase.app.App for the Firebase App to use
  • collectionPath: (Optional) string to specify the Cloud Firestore collection where devices should be stored. Defaults to user-devices.

Returns: Promise<DeviceStore> containing:

  • signOut: A method to be called before firebase.auth().signOut() to ensure that the device token is removed from the user. This can't be done automatically due to Cloud Firestore security rules.
  • unsubscribe: A method that can be called to unsubscribe the device store from Auth and Messaging.

Security rules

You will need to add the following security rules for your Cloud Firestore collection:

service cloud.firestore {
  match /databases/{database}/documents {
    // Add this rule, replacing `user-devices` with the collection path you would like to use:
    match /user-devices/{userId} {
      allow create, read, update, delete: if request.auth.uid == userId;
    }
  }
}