只允许对Firebase的Cloud

只允许对Firebase的Cloud

本文介绍了只允许对Firebase的Cloud Functions进行写入权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何通过只允许Firebase的云端函数将数据写入特定位置的规则来保护数据库,以前有一个选项可以将管理客户端添加到 databaseAuthVariableOverride 并在规则部分使用该uid,但现在我们初始化通过 admin.initializeApp(functions.config()。firebase); 所以我不知道如何添加额外的参数。

编辑
这是一个好主意,而不是使用证书而不是?即

  admin.initializeApp({
凭证:admin.credential.cert(/ path-to-cert ),
databaseURL:database-url,
databaseAuthVariableOverride:{uid:some-id}
});

admin.initializeApp(functions.config()。firebase) 已经在上面了,其中 functions.config()实际从中获取数据,是不是只是一个节点模块?

解决方案

通常情况下,在您的云端函数代码中,您有:

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

作为 firebase-functions 节点的一部分模块,您可以访问这只是一个对象,它具有初始化Admin SDK所需的一切,包括您的数据库URL和凭据实现(基于应用程序默认凭证)。如果你在你的代码中 console.log(functions.config()。firebase),你会看到它只是一个包含这些属性的对象,还有一些你可能想要的在您的代码中使用。



您可以添加到复制相关的细节到一个新的对象:

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


How can we secure database via the rules that only allow Cloud Functions for Firebase to write data to certain locations, previously there was an option to add uid to admin client databaseAuthVariableOverride and use that uid in rules section, but now we initialise via admin.initializeApp(functions.config().firebase); so I’m not to sure about how to add additional params in.

EDITis it a good idea to initiate with certificate for this instead? i.e

admin.initializeApp({
  credential: admin.credential.cert("/path-to-cert"),
  databaseURL: "database-url",
  databaseAuthVariableOverride: { uid: "some-id" }
});

What benefit does admin.initializeApp(functions.config().firebase) have over above and where is functions.config() actually getting data from, isn't this just a node module?

解决方案

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);

这篇关于只允许对Firebase的Cloud Functions进行写入权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 10:53