3
votes

I am writing an app using firebase as a backend, and I am trying to write to the database, but am getting "Error 7: Missing or insufficient permissions." Someone told me cloud functions bypasses the firebase permissions, so I'm not sure what could be causing this error. Can someone please take a look?

Here are my permissions:

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

and here is my code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
var express = require('express');
admin.initializeApp(functions.config().firebase);

const app = express();

let db = admin.firestore();

app.get('/helloworld', (req, res) => res.send('Hello World!'));

app.post('/signup', (req, res) => {


  var email = req.body.email;
  var username = req.body.username;
  var password = req.body.password;


  //creating document.  Here is where it isn't working

  let docRef = db.collection('UsersMain').doc('firstdoc');

    let data = {
    Email: '[email protected]',
    UserName: 'Matt'
  };

  let setDoc = docRef.set(data).then(() =>  {
    res.send('Login Complete');
  })
  .catch(function(error) {
    console.error("Error adding document: ", error);
});

});



const api1 = functions.https.onRequest(app);

module.exports = {api1};

and here is the error I'm getting:

{ Error: 7 PERMISSION_DENIED: Missing or insufficient permissions.
>      at Object.callErrorFromStatus (/Users/mg8686./Desktop/mattsapp/mattsappfinal/backend/functions/node_modules/@grpc/grpc-js/build/src/call.js:30:26)
>      at Http2CallStream.call.on (/Users/mg8686./Desktop/mattsapp/mattsappfinal/backend/functions/node_modules/@grpc/grpc-js/build/src/client.js:96:33)
>      at Http2CallStream.emit (events.js:203:15)
>      at process.nextTick (/Users/mg8686./Desktop/mattsapp/mattsappfinal/backend/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:97:22)
>      at process._tickCallback (internal/process/next_tick.js:61:11)
>    code: 7,
>    details: 'Missing or insufficient permissions.',
>    metadata: Metadata { internalRepr: Map {}, options: {} } }


Can someone please take a look? Thanks!

2
not sure if it'll work, try initialising admin as admin.initializeApp(); instead of admin.initializeApp(functions.config().firebase); - ked
Hi @ked, thanks for your help! I am still getting a Error: 7 PERMISSION_DENIED error - Matt123
Where exactly are you getting this error? In the Cloud Functions console? In the browser when you call one of the endpoints you expose through the Cloud Function? - Renaud Tarnec

2 Answers

0
votes

@Matias Seguel's solution worked for me listed here: Firebase Cloud Functions Firestore Trigger produces: Error: 7 PERMISSION_DENIED: Missing or insufficient permissions

It isn't a great production solution but for development purposes it works.

-1
votes

This seems to be quiet a common error that seems to have multiple different solutions, that would depend on what you are worrying at the moment.

For example, there is a simple solution that could help you momentously, but it's not very recommended, that is using the below configuration:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

For this reason, I would recommend you to take a look at the below links, to verify possible alternatives and which one would fit you better.

Let me know if the information helped you!