【问题标题】:Enable a Firebase user to change it's Authentication Provider? [duplicate]允许 Firebase 用户更改其身份验证提供程序? [复制]
【发布时间】:2015-05-21 15:04:15
【问题描述】:

假设我有一个这样的数据结构:

{
  "users" : {
    "google:1234567890" : {
      "displayName" : "Username A",
      "provider" : "google",
      "provider_id" : "1234567890"
    }, ...
  },
  "todos" : {
    "google:1234567890" : {
      "rec_id1" : {
        "todo" : "Walk the dog"
      },
      "rec_id2" : {
        "todo" : "Buy milk"
      },
      "rec_id3" : {
        "todo" : "Win a gold medal in the Olympics"
      }, ...
    }, ...
  }
}

然后我只允许用户使用以下安全规则写入/读取自己的数据:

{
  "rules": {
    "users": {
      "$uid": {
        // grants write and read access to the owner of this user account whose uid must exactly match the key ($uid)
        ".write": "auth !== null && auth.uid === $uid",
        ".read": "auth !== null && auth.uid === $uid"
      }
    },
    "todos": {
      "$uid": {
        // grants write and read access to the owner of this user account whose uid must exactly match the key ($uid)
        ".write": "auth !== null && auth.uid === $uid",
        ".read": "auth !== null && auth.uid === $uid"
      }
    }
  }
}

如果用户想要将 Authentication Provider 更改为例如从 Google 更改为 Facebook,会发生什么情况,因为这也会更改他们的 auth.uid - 有没有办法在用户无法访问之前的数据的情况下完成此操作?

【问题讨论】:

  • 类似讨论:1234
  • @Kato:感谢之前没有看到的相关讨论。

标签: android json data-structures firebase firebase-security


【解决方案1】:

您需要一种方法让应用程序识别 Facebook 和 Google 帐户是针对同一用户的,例如通过电子邮件地址。

找到这种方式后,您只需将数据从一个帐户迁移到另一个帐户即可。有关如何执行此操作的示例,请参见此答案:https://stackoverflow.com/a/29124564/209103

【讨论】:

  • 感谢您的回答,弗兰克。我不知道为所有子节点更新现有节点是如此容易。但是为了让用户能够使用多个帐户登录,我认为我会更喜欢@Kato 发布的this method
【解决方案2】:

我认为我更喜欢@Kato 在 cmets 中发布的this method,而不是迁移数据。

假设用户首先通过他的 Google 帐户注册,然后在应用程序中,他将有一个选项,例如“将此应用程序与 Facebook 连接”。

如果他这样做,我可以添加一个额外的根的userMappings 子节点,用户只有在他当前的 auth.id 与子名称匹配时才能访问该子节点。在其中我可以存储他第一次使用的(Google)用户 ID。

这样做的好处是用户可以添加多个身份验证提供程序。

【讨论】:

    猜你喜欢
    • 2018-10-31
    • 1970-01-01
    • 2023-03-15
    • 2018-02-25
    • 2020-08-21
    • 1970-01-01
    • 2017-12-25
    • 2017-04-16
    • 2016-05-05
    相关资源
    最近更新 更多