【问题标题】:Is there a way to upload an image to Firebase Storage based on Firestore values?有没有办法根据 Firestore 值将图像上传到 Firebase 存储?
【发布时间】:2020-02-28 00:48:40
【问题描述】:

我认为使用 Firebase 会很简单,但我还没有找到解决方案:

我的 Firestore 数据库中有一个 Admins 集合,基本上想限制 Firebase 存储中的 Images 文件夹,以便只有 Admins 可以添加新图像。 安全规则不允许我执行“如果用户 ID 在数据库中则允许创建”。

有谁知道如何实现这个功能?从查看其他问题看来,函数可能能够做到这一点,但是我找不到关于如何使用函数上传图像的有效解决方案,更不用说根据数据库数据限制它了。

【问题讨论】:

  • 目前无法在 Cloud Storage 安全规则中使用 Cloud Firestore 文档,但您可以使用 Michael 提供的解决方法,以便将管理员状态镜像到 Firebase Auth 帐户的自定义声明中。跨度>
  • 感谢您对 Doug 的确认!

标签: firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

我为类似案例所做的事情是使用云函数将管理员状态同步到Firebase Auth custom claims。简化版本可能类似于:

exports.setAdminClaim = functions.firestore.document('admins/{uid}').onWrite(change => {
  if (change.after.exists) {
    return admin.auth().setCustomUserClaims(change.after.id, {admin: true});
  }

  return admin.auth().setCustomUserClaims(change.after.id, {admin: false});
});

这允许您在存储规则中使用自定义声明:

allow write: if request.auth.token.admin == true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-23
    • 2019-08-31
    • 1970-01-01
    • 2019-07-24
    • 2017-09-25
    相关资源
    最近更新 更多