Giter VIP home page Giter VIP logo

csfrequency / firebase-device-store-js-sdk Goto Github PK

View Code? Open in Web Editor NEW
7.0 4.0 1.0 265 KB

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

License: Apache License 2.0

JavaScript 14.49% TypeScript 85.51%
firebase firebase-auth firebase-authentication firebase-messaging firebase-cloud-messaging firebase-firestore firebase-cloud-firestore javascript typescript

firebase-device-store-js-sdk's Introduction

Firebase Device Store (Javascript SDK)

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 createDeviceStore from 'firebase-device-store';

const deviceStore = createDeviceStore(firebase.app(), 'user-devices');
await deviceStore.subscribe();

Documentation

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

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: {
    deviceId1: Device,
    deviceId2: Device,
    ...
  },
  userId: string,
}

A Device object contains the following:

{
  deviceId: string, // A randomly generated ID
  fcmToken: string, // The FCM token
  name: string,     // The browser name
  os: string,       // The OS of the device
  type: 'Web',
  userAgent: string // The browser user agent string
}

API

createDeviceStore(app, collectionPath)

Create a new DeviceStore.

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 a DeviceStore.

DeviceStore.signOut(): Promise<void>

Indicate to the DeviceStore that the user is about to sign out, and the current device token should be removed.

This can't be done automatically with onAuthStateChanged as the user is already signed out at this point. This means the Cloud Firestore security rules will prevent the database deletion as they no longer have the correct user permissions to remove the token.

DeviceStore.subscribe(): Promise<void>

Subscribe a device store to the Firebase App. This will:

  1. Request appropriate Firebase Cloud 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 Cloud Messaging and listen to changes in the FCM token
  4. Automatically store device and FCM token information in the Cloud Firestore collection you specify

DeviceStore.unsubscribe(): void

Unsubscribe the device store from the Firebase App.

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

firebase-device-store-js-sdk's People

Contributors

chrisbianca avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

klutchdev

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.