firebase 9 permission denide from localhost

Viewed 236

i'm using Firebase version 9 (MODULAR) as database for a web application in nodejs v18.

When i try to access data from localhost i get the error below, but from an heroku deployment works like a charm.

[2022-06-26T07:32:22.447Z]  @firebase/firestore: Firestore (9.8.4): Connection GRPC stream error. Code: 7 Message: 7 PERMISSION_DENIED: Permission denied on resource project "my_project",.
[2022-06-26T07:32:22.447Z]  @firebase/firestore: Firestore (9.8.4): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=permission-denied]: 7 PERMISSION_DENIED: Permission denied on resource project "my_project",.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
[2022-06-26T07:32:22.466Z]  @firebase/firestore: Firestore (9.8.4): Connection GRPC stream error. Code: 7 Message: 7 PERMISSION_DENIED: Permission denied on resource project "my_project",.
node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[FirebaseError: Failed to get document because the client is offline.] {
  code: 'unavailable',
  customData: undefined,
  toString: [Function (anonymous)]
}

Node.js v18.0.0

For context: the web app its an internal tool so i do my own authentication server-side and then the app makes some checks on some data on the firestore db. So basically who makes the queries its the app,not the user. This makes me thing that i should use a service account with firestore-admin api. The problem is that the provided code snippet from the docs,does not work for me..

var admin = require("firebase-admin");

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

And i already changed the access rule to be the horribly unsecure one:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

Any idea on what i should do? Or maybe why it doesnt work on local development? Since the documentation it an incomplete maze of stuff i am probably missing something..

Thanks to anyone that can help! (at least to understand why)

1 Answers

As the GitHub link suggests, the issue might be due to useFetchStreams being enabled by default, in the new modular build (v9).

Hence, modify your code accordingly:

import {initializeFirestore} from 'firebase/firestore'
const db = initializeFirestore(firebaseApp, {useFetchStreams: false})

Also, the error might arouse not because the android device or emulator you are debugging is offline but because the "databaseURL" setting was missing in the part that defines the Firebase configuration object. I think that the same error might occur if the setting is incorrect.

Also, I suggest you have a look at this Stackoverflow case, which talks in detail about all the probable reasons behind the ‘Client is offline’ issue.

However, if the above solution does not, suggest you enable debug logging by calling setLogLevel.

firebase.firestore.setLogLevel('debug');
Related