【问题标题】:Firebase database authenticationFirebase 数据库身份验证
【发布时间】:2017-06-05 06:09:58
【问题描述】:

我正在尝试在用户的用户 ID 下将文本 (32424234) 添加到我的 Firebase 中,但它不允许我这样做。

这是我在 firebase 下的规则部分

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

这是我的java代码:

public void kullanicikontrol(String kullaniciadi){
        mRootRef = new Firebase("https://xxxx.firebaseio.com/users");
        Firebase chieldref = mRootRef.child(kullaniciadi);
        chieldref.setValue("32424234");
}

如果我改变规则如 ".read": true" 就没有问题,如下图所示:

但我不想。我做错了什么?

【问题讨论】:

  • 您在下方评论中添加的代码使用“新”Firebase SDK(在 I/O 2016 推出)。您问题中的代码使用早于该版本的旧 Firebase SDK。较新 SDK 中的身份验证不适用于使用较旧 SDK 的数据库访问。要解决此问题,请更新您的数据库代码以使用新的 SDK:firebase.google.com/docs/database

标签: android firebase firebase-realtime-database firebase-security


【解决方案1】:

在 firebase 中 .read 和 .write 是读写权限。如果将其设置为 true,则允许所有用户。

您可以通过以下链接创建新用户和身份验证 https://firebase.google.com/docs/auth/web/manage-users

我希望这会对你有所帮助。

【讨论】:

  • 我得到的 uid 与你提到的帖子有点不同。 FirebaseUser 用户 = FirebaseAuth.getInstance().getCurrentUser(); if (user != null) { // 姓名、电子邮件地址和头像 Url String name = user.getDisplayName();字符串电子邮件 = user.getEmail(); Uri photoUrl = user.getPhotoUrl();字符串 uid = user.getUid(); kullanici = 新数据库(); kullanici.kullanicikontrol(uid); } 错了吗?
  • 用户 ID,Firebase 项目独有。如果您有后端服务器,请勿使用此值对后端服务器进行身份验证。改用 User.getToken()。
猜你喜欢
  • 2019-04-19
  • 1970-01-01
  • 2019-04-25
  • 2020-09-24
  • 2017-12-26
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 2018-08-21
相关资源
最近更新 更多