Firestore Missing or insufficient Permissions

Asked

Viewed 637 times

-1

The same code if I make it inside an html page, it works:

 <script src="https://www.gstatic.com/firebasejs/7.19.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.19.0/firebase-firestore.js"></script>
    <script>

      var firebaseConfig = {
        apiKey: "XYZ",
        authDomain: "XYZ.firebaseapp.com",
        databaseURL: "https://XYZ.firebaseio.com",
        projectId: "XYZ",
        storageBucket: "XYZ.appspot.com",
        messagingSenderId: "XYZ",
        appId: "XYZ"
      };
      // Initialize Firebase
      let db =null;
      if(!firebase.apps.length){
        firebase.initializeApp(firebaseConfig);
        db = firebase.firestore();
      }

      const id = new Date().getTime();
      const userid = new Date().getTime();
      let docRef = db.collection("alarms").doc("teste"+id);

      docRef.set({ userid: userid  }, { merge: true }).then((id)=>{
        console.log("ID:",id);
      }).catch(
        (err)=>{
          console.error(err);
        }
      );

Above, replace the sensitive information by XYZ to put here on the site, if you need to put your own settings.

The example above I picked from google and works perfectly.

Already, inside the Node server, in the backend, using the following dependencies:

package json.

   {
  "name": "appengine-typescript",
  "description": "An example TypeScript app running on Google App Engine.",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": ">=8.0.0"
  },
  "scripts": {
    "start": "node -r source-map-support/register index.js"
  },
  "dependencies": {
    "async_hooks": "^1.0.0",
    "axios": "^0.19.2",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "ejs": "^2.6.2",
    "express": "^4.16.3",
    "firebase-admin": "8.6.0",
    "firebase-functions": "3.3.0",
    "jest-cli": "^26.4.2",
    "js-sha1": "^0.6.0",
    "node-cache": "^4.2.1",
    "node-rest-client": "^3.1.0",
    "nodemailer": "^6.3.0",
    "source-map-support": "^0.5.16",
    "uuid": "^3.3.2"
  },
  "devDependencies": {
  },      
}

And with the following code:

import * as functions from "firebase-functions";
import * as firebase from "firebase-admin";

 if (!firebase.apps.length) {
    var firebaseConfig = {
        apiKey: "XYZ",
        authDomain: "XYZ.firebaseapp.com",
        databaseURL: "https://XYZ.firebaseio.com",
        projectId: "XYZ",
        storageBucket: "XYZ.appspot.com",
        messagingSenderId: "XYZ",
        appId: "XYZ"
      };
      // Initialize Firebase
      firebase.initializeApp(firebaseConfig); 
}

const db = firebase.firestore();

const cmpID = "cmpid:" + new Date().getTime();
const userid = "userid" + new Date().getTime();


let docRef = db.collection("alarms").doc(cmpID);

docRef.set({ userid  }, { merge: true }).then((id)=>{
    console.log("ID:",id);
  }).catch(
    (err)=>{
      console.error(err);
    }
  );

In this scenario, the following error occurs:

{ Error: 7 PERMISSION_DENIED: Missing or insufficient permissions.
    at Object.callErrorFromStatus (/mnt/c/Users/gandb/Documents/workspace/vriend/v-alarm/services/node_modules/@grpc/grpc-js/build/src/call.js:30:26)
    at Http2CallStream.call.on (/mnt/c/Users/gandb/Documents/workspace/vriend/v-alarm/services/node_modules/@grpc/grpc-js/build/src/client.js:96:33)
    at Http2CallStream.emit (events.js:203:15)
    at process.nextTick (/mnt/c/Users/gandb/Documents/workspace/vriend/v-alarm/services/node_modules/@grpc/grpc-js/build/src/call-stream.js:100:22)
    at process._tickCallback (internal/process/next_tick.js:61:11)
  code: 7,
  details: 'Missing or insufficient permissions.',
  metadata: Metadata { internalRepr: Map {}, options: {} } }

Firestore permissions are like this:

service cloud.firestore {
    match /databases/{database}/documents {
    match /{document=**} {
      allow  create,read, write, update, delete: if true;
    }
    }
}

What am I doing wrong in Node JS version? You have to do something else to work, missed importing some dependency?

  • 1

    On the way, it looks like you are using WSL. You could try moving the /mnt/c folder to your Linux home?

  • Hi Julio. I switched to my home and the error remains, only now the path points to the home (/home/gandbanco/temp/node_modules/@grpc/grpc-js/build/src/call.js:30:26) , due to the doubts I did even a test to change the home directory permission to 777 but it seems to have no relation with directory permission, because the error has not changed.

  • 1

    Then there must be a lack of permissions in Firebase. Check in Firebase Console if you have the read and write rules as true. Look at this thread: https://stackoverflow.com/questions/37403747/firebase-permission-denied

  • I added in the post the rules of firebase, I do not believe it is the rules because even with anonymity window, Google, when the code is in html works, but nodejs not.

1 answer

0


I discovered the error, this message occurs for several different reasons, one of which I have not found documented anywhere, even in stacoverflow in English, occurs when you authenticate in google sdk with a user who has permission for another project, that easily occurs if you generated the credentials for one project and unintentionally used in another, in my case I used Ocker and created an environment variable that pointed to the json file of credentials of the wrong project, as for example:

ENV GOOGLE_APPLICATION_CREDENTIALS projeto-errado.json

In html it works because it does not need the credential, but if you are going to run in nodejs firestore requires the correct credential to work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.