jofftiquez
jofftiquez

Reputation: 7708

Cloud Functions for Firebase - Getting permission denied even with admin initialized

I setup my cloud functions with firebase-admin like :

const admin = require('firebase-admin');
const fn = require('firebase-functions');

admin.initializeApp(fn.config().firebase);

However I am getting permission denied upon writing on the database. What's weird is it only happens to some collection but not to all of them. Some works, some won't.

My understanding of admin.initializeApp(fn.config().firebase); is that this will allow my cloud functions to have an absolute power over the database regardless of the security rules.

Here's the error:

enter image description here

EDIT

I write the data like this.

exports.foo = fn.database.ref('some-path').onWrite(e => {
    // some handling
    const ref = e.data.ref;

    return ref.child('bar').set('some-data').then( // ).catch( // );
})

Upvotes: 3

Views: 1268

Answers (1)

Bob Snyder
Bob Snyder

Reputation: 38309

To obtain full access to the database from the DeltaSnapshot provided in an event, use adminRef:

Returns a Reference to the Database location where the triggering write occurred. Similar to ref, but with full read and write access instead of end-user access

exports.foo = fn.database.ref('some-path').onWrite(e => {
    // some handling
    const ref = e.data.adminRef;  // <== CHANGED

    return ref.child('bar').set('some-data').then( // ).catch( // );
})

Upvotes: 6

Related Questions