【问题标题】:Authenticate node.js server with Firebase Realtime DB使用 Firebase Realtime DB 验证 node.js 服务器
【发布时间】:2019-05-10 13:34:25
【问题描述】:

我想验证我的 node.js 应用程序以将数据写入 Firebase 实时数据库并允许客户端读取。我现在的数据库架构的结构是:

"<db_name>": {
  "users": { ... }
}

这是我目前得到的规则配置:

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

这不应该至少允许对数据库的所有写访问吗? 而且,我尝试搜索有关如何使用 Firebase DB 对服务器应用程序进行身份验证的官方文档,但我只能找到如何使用 Firebase Admin(这不是我需要的)。

在我的代码中:

const firebaseConfig = {
  "apiKey": "xxx",
  "authDomain": "xxx.firebaseapp.com",
  "databaseURL": "https://xxx.firebaseio.com",
  "projectId": "xxx",
  "storageBucket": "xxx.appspot.com",
  "messagingSenderId": "xxx"
}
firebase.initializeApp(firebaseConfig);

但这只是初始化连接。我还需要以某种方式使用 Firebase 授权我的服务器,通过持久令牌或 smth,我不确定......

Here's 我发现的唯一明智的东西,但它是从 2015 年开始的,API 已经改变。

【问题讨论】:

  • 客户端节点在指南中确实没有很好地记录。有关该平台的完整参考文档,请参阅firebase.google.com/docs/reference/node。有一个signInWithEmailAndPassword method,应该允许您以普通用户身份登录。
  • 嗨 Milkncookiez,你有没有找到解决方案?我还想对我的服务器进行身份验证,以便在我设置诸如 auth !== null 之类的规则后授权我的服务器进行读/写

标签: node.js firebase firebase-realtime-database firebase-authentication firebase-security


【解决方案1】:

大部分文档都侧重于在受信任的环境中使用 Node.js,您可以在其中使用 Admin SDK 获得完全的管理访问权限。如果您使用的是受信任的平台,我建议您使用该平台,然后将 setDatabaseAuthVariableOverride 致电至 access the database with limited privileges

有关不受信任环境中客户端 node.js 的参考文档,请参阅https://firebase.google.com/docs/reference/node/。有一个signInWithEmailAndPassword method,应该允许您以普通用户身份登录。

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2017-07-16
    • 2016-12-04
    • 2021-12-18
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    相关资源
    最近更新 更多