Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

firebase-device-store

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

firebase-device-store

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

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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;
    }
  }
}

FAQs

Package last updated on 09 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc