{"id":18996301,"url":"https://github.com/csfrequency/firebase-device-store-js-sdk","last_synced_at":"2025-04-22T14:02:18.382Z","repository":{"id":44850590,"uuid":"186972817","full_name":"CSFrequency/firebase-device-store-js-sdk","owner":"CSFrequency","description":"Automatically store Device and FCM Token information for Firebase Auth Users in Cloud Firestore","archived":false,"fork":false,"pushed_at":"2022-12-03T13:37:47.000Z","size":265,"stargazers_count":7,"open_issues_count":8,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-17T03:09:21.457Z","etag":null,"topics":["firebase","firebase-auth","firebase-authentication","firebase-cloud-firestore","firebase-cloud-messaging","firebase-firestore","firebase-messaging","javascript","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CSFrequency.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2019-05-16T07:14:00.000Z","updated_at":"2023-08-15T00:00:21.000Z","dependencies_parsed_at":"2023-01-23T22:47:48.175Z","dependency_job_id":null,"html_url":"https://github.com/CSFrequency/firebase-device-store-js-sdk","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CSFrequency%2Ffirebase-device-store-js-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CSFrequency%2Ffirebase-device-store-js-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CSFrequency%2Ffirebase-device-store-js-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CSFrequency%2Ffirebase-device-store-js-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CSFrequency","download_url":"https://codeload.github.com/CSFrequency/firebase-device-store-js-sdk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250255704,"owners_count":21400411,"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","firebase-authentication","firebase-cloud-firestore","firebase-cloud-messaging","firebase-firestore","firebase-messaging","javascript","typescript"],"created_at":"2024-11-08T17:34:36.058Z","updated_at":"2025-04-22T14:02:18.329Z","avatar_url":"https://github.com/CSFrequency.png","language":"TypeScript","readme":"# Firebase Device Store (Javascript SDK)\n\nAutomatically store Device and FCM Token information for Firebase Auth Users in Cloud Firestore.\n\n[![npm version](https://img.shields.io/npm/v/firebase-device-store.svg?style=flat-square)](https://www.npmjs.com/package/firebase-device-store)\n[![npm downloads](https://img.shields.io/npm/dm/firebase-device-store.svg?style=flat-square)](https://www.npmjs.com/package/firebase-device-store)\n\n\u003e This library is a proof of concept, and very much a work in progress.\n\n## Installation\n\nFirebase Device Store requires **Firebase v5.0.0 or later**.\n\n```\nnpm install --save firebase-device-store\n```\n\n## Setup\n\nThe following Firebase libraries need to be enabled in your application:\n\n```\nimport firebase from 'firebase/app';\nimport 'firebase/auth';\nimport 'firebase/firestore';\nimport 'firebase/messaging';\n```\n\n## Example usage\n\n```\nimport createDeviceStore from 'firebase-device-store';\n\nconst deviceStore = createDeviceStore(firebase.app(), 'user-devices');\nawait deviceStore.subscribe();\n```\n\n## Documentation\n\nFirebase Device Store automatically stores device and FCM information for Firebase Auth users in Cloud Firestore.\n\n### Data Model\n\nA Document is created in the Cloud Firestore collection for each logged in user:\n\n```\n/user-devices\n  - userId1: {},\n  - userId2: {},\n```\n\nThe structure of this Document is as follows:\n\n```\n{\n  devices: {\n    deviceId1: Device,\n    deviceId2: Device,\n    ...\n  },\n  userId: string,\n}\n```\n\nA `Device` object contains the following:\n\n```\n{\n  deviceId: string, // A randomly generated ID\n  fcmToken: string, // The FCM token\n  name: string,     // The browser name\n  os: string,       // The OS of the device\n  type: 'Web',\n  userAgent: string // The browser user agent string\n}\n```\n\n### API\n\n#### `createDeviceStore(app, collectionPath)`\n\nCreate a new DeviceStore.\n\nParameters:\n\n- `app`: `firebase.app.App` for the Firebase App to use\n- `collectionPath`: (Optional) `string` to specify the Cloud Firestore collection where devices should be stored. Defaults to `user-devices`.\n\nReturns a `DeviceStore`.\n\n#### `DeviceStore.signOut(): Promise\u003cvoid\u003e`\n\nIndicate to the DeviceStore that the user is about to sign out, and the current device token should be removed.\n\nThis 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.\n\n#### `DeviceStore.subscribe(): Promise\u003cvoid\u003e`\n\nSubscribe a device store to the Firebase App. This will:\n\n1. Request appropriate Firebase Cloud Messaging permissions, if they have not already been granted\n2. Subscribe to Firebase Auth and listen to changes in authentication state\n3. Subscribe to Firebase Cloud Messaging and listen to changes in the FCM token\n4. Automatically store device and FCM token information in the Cloud Firestore collection you specify\n\n#### `DeviceStore.unsubscribe(): void`\n\nUnsubscribe the device store from the Firebase App.\n\n### Security rules\n\nYou will need to add the following security rules for your Cloud Firestore collection:\n\n```\nservice cloud.firestore {\n  match /databases/{database}/documents {\n    // Add this rule, replacing `user-devices` with the collection path you would like to use:\n    match /user-devices/{userId} {\n      allow create, read, update, delete: if request.auth.uid == userId;\n    }\n  }\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsfrequency%2Ffirebase-device-store-js-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcsfrequency%2Ffirebase-device-store-js-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsfrequency%2Ffirebase-device-store-js-sdk/lists"}