【问题标题】:Firebase error: Permission denied. Unable to read/write from Firebase DatabaseFirebase 错误:权限被拒绝。无法从 Firebase 数据库读取/写入
【发布时间】:2017-01-27 18:54:27
【问题描述】:

我只需要向经过身份验证的用户授予读/写访问权限,但 Firebase 似乎无法识别用户已通过身份验证。

在我使用电子邮件和密码登录用户后,我假设用户已通过身份验证,并且应该被允许从数据库读取/写入。但是权限被拒绝。

这是登录活动的代码:

 firebaseAuth.signInWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            //checking if success
            if(task.isSuccessful()){
                //display some message here
                // Toast.makeText(Register.this,"Successfully registered",Toast.LENGTH_LONG).show();


                sRef.authWithPassword(email,password,new Firebase.AuthResultHandler() {

                    @Override
                    public void onAuthenticated(AuthData authData) {
                        Toast.makeText(Register.this, user.getDisplayName()+" is authenticated",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onAuthenticationError(FirebaseError firebaseError) {
                    }
                });

                Intent i = new Intent(Register.this,MainActivity.class);

                startActivity(i);

            }else{
                //display some message here
                Toast.makeText(Register.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
            }

        }
    });

我想在 firebase 中使用默认规则,只允许授权用户从数据库读取/写入:

{
 "rules": {
  ".read": "auth != null",
 ".write": "auth != null"
}
}

但是当我更改如下规则时,我的代码能够获得对数据库的读/写访问权限。

{
"rules": {
".read": true,
".write":true
}
}

这在测试中很好,但是我不能用这种方法继续生产。请帮助,如何验证注册用户以访问 Firebase 中的数据库。

【问题讨论】:

  • 您共享的代码都没有访问数据库,因此它还不能引发该错误。请分享minimum, complete code that is needed to reproduce the problem
  • 您是否在登录前在 auth 部分启用了 firbase 登录方法,例如电子邮件/密码或匿名。
  • 我没有分享访问 firebase 数据库的代码,因为它正在工作,但只有当我将规则更改为公共时。当我使用默认规则时,权限被拒绝。
  • @DhavalkumarSolanki 是的,我都做过。

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


【解决方案1】:

您的代码混合了来自旧版 2.x.x 的 API 调用。 SDK,例如:

sRef.authWithPassword(email,password,new Firebase.AuthResultHandler()

以及新的 9.x.x SDK:

firebaseAuth.signInWithEmailAndPassword()

这两个 SDK 不兼容。您需要使用或其他。 Upgrade Guide 解释了如何从旧版 SDK 迁移到新版 SDK。

您还需要使用与所用 SDK 对应的 Firebase 控制台:Legacy Console 或新的Firebase Console

【讨论】:

  • 我确实尝试删除:sRef.authWithPassword(email,password,new Firebase.AuthResultHandler() 但它仍然不起作用。这是我做的第一件事。
  • 我得到了它的工作,我所要做的就是更新 android studio 存储库并添加新的依赖项。谢谢。
猜你喜欢
  • 2019-04-27
  • 1970-01-01
  • 1970-01-01
  • 2019-02-25
  • 2016-08-27
  • 2017-04-17
  • 2016-11-15
  • 1970-01-01
  • 2016-09-22
相关资源
最近更新 更多