在节点上使用admin SDK访问实时数据库时如何指定有效的uid [英] How to specify an effective uid when accessing Realtime Database with the admin SDK on node

查看:73
本文介绍了在节点上使用admin SDK访问实时数据库时如何指定有效的uid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是node.js的新手.我已经创建了一个带有用户节点的数据库. 用户"节点受以下规则保护.

I am new to node.js. I have a created a database with users node. Users node is protected with the following rules.

{
  "rules": {
    "users":{
            "$userId":{
        ".read" : "auth.uid === $userId",
          ".write" : true
      }
    }
  }
}

要从节点js访问用户节点下的数据,我需要在身份验证令牌有效负载中发送uid.请通过示例代码帮助我,以通过节点js将有效负载发送到Firebase.

To access data under users node from node js, I would need to send the uid in auth token payload. Please help me with an example code to send the payload to firebase via node js.

推荐答案

在服务器上,当您初始化Firebase应用时,请使用 databaseAuthVariableOverride选项覆盖使用的auth对象 根据您的数据库规则.在此自定义身份验证对象中,设置uid字段 到您用来表示安全性"中的服务的标识符 规则.

On your server, when you initialize the Firebase app, use the databaseAuthVariableOverride option to override the auth object used by your database rules. In this custom auth object, set the uid field to the identifier you used to represent your service in your Security Rules.

// Initialize the app with a custom auth variable, limiting the server's access
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://databaseName.firebaseio.com",
  databaseAuthVariableOverride: {
    uid: "my-service-worker"
  }
});

这篇关于在节点上使用admin SDK访问实时数据库时如何指定有效的uid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆