Allowing write access only to Cloud Functions for Firebase

若如初见. 提交于 2019-11-29 02:34:08

Normally, at the top of your Cloud Functions code, you have:

var functions = require('firebase-functions');

As part of the firebase-functions node module, you have access to a functions.config().firebase which is just an object which has everything you need to initialize the Admin SDKs, including your Database URL and a credential implementation (based off of Application Default Credentials). If you console.log(functions.config().firebase) in your code, you will see it just is an object with these properties and a few other ones you may want to use in your code.

You can add databaseAuthVariableOverride to this object to limit the Admin SDK's privileges. You can just overwrite the object itself:

var firebaseConfig = functions.config().firebase;
firebaseConfig.databaseAuthVariableOverride = {
  uid: 'some-uid',
  foo: true,
  bar: false
};
admin.initializeApp(firebaseConfig);

Or you can use something like Object.assign() to copy the relevant details to a new object:

var firebaseConfig = Object.assign({}, functions.config().firebase, {
  databaseAuthVariableOverride: {
    uid: 'some-uid',
    foo: true,
    bar: false
  }
});
admin.initializeApp(firebaseConfig);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!