Matt123
Matt123

Reputation: 303

Error 7: Missing or insufficient permissions cloud functions/firestore

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!

Upvotes: 5

Views: 14487

Answers (1)

Matt123
Matt123

Reputation: 303

@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.

Upvotes: 1

Related Questions