【问题标题】:how do resolve firebase permission denied error on android?如何解决android上的firebase权限被拒绝错误?
【发布时间】:2020-11-08 04:48:18
【问题描述】:

该应用在尝试访问 Firebase 数据库的位置一直挂起。 这是应用程序尝试访问数据库的代码。

     mAuth = FirebaseAuth.getInstance();
        Log.d("getUid()", mAuth.getCurrentUser().getUid());
        databaseReference = FirebaseDatabase.getInstance().getReference();
        final AlertDialog a = Utiles.createLoadingDialog(getActivity(), "Loading..");
        databaseReference.child("bibles").orderByChild("likecount").addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                bList.clear();
                bList2.clear();
                for (DataSnapshot i : dataSnapshot.getChildren()) {
                    Bible b = i.getValue(Bible.class);
                    // System.out.println("TTTTT" + b.toString());


                    b.setKey(i.getKey());
                    if (b.getCategory().equals(value)) {
                        if (b.getIsChecked().get(mAuth.getCurrentUser().getUid()) == null) {
                            bList2.add(b);

                            // Log.d("TAG", bList2.toString());
                            continue;
                        }

                        bList.add(b);
                    }
                }

我的应用显示它一直在加载。并且错误日志说

E/fragment1:com.google.firebase.database.DatabaseException:Firebase 数据库错误:权限被拒绝

安全规则:

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

当我关闭我的应用程序并重新打开它时,它可以访问数据库,因此它可以完美运行,并在我的仪表板上创建一个新的身份验证 uid。

当我查看创建当前用户的代码时。 这是代码。正如您在下面的代码中看到的,有一个名为“!!!mCurrentUser”的日志。新安装应用程序时不应打印此日志。 但是在我删除应用程序并重新安装后,应用程序以某种方式打印出“!!!!mCurrentUser”。所以我猜测即使我删除了应用程序,firebase 数据库系统也会以某种方式将我的手机识别为当前用户。有人对此有什么想法吗?提前谢谢你。

    mAuth = FirebaseAuth.getInstance();
    mCurrentUser = mAuth.getCurrentUser();

    if (mCurrentUser == null) {
        Log.d("UserNull", "test");
        mAuth.signInAnonymously().addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    Log.d("signIn", "test");
                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                        mInterstitialAd.setAdListener(new AdListener() {
                            @Override
                            public void onAdClosed() {
                                //TODO: Intent
                                //
                                Intent intent = new Intent(getBaseContext(), MainActivity.class);
                                startActivity(intent);


                            }
                        });
                    } else {
                        //TODO: Intent
                        Intent intent = new Intent(getBaseContext(), MainActivity.class);
                        startActivity(intent);



                    }

                } else {
                    Log.d("authentication failed", "failed");
                }
            }
        });
    } else {
        Log.d("!!!!mCurrentUser", String.valueOf(mCurrentUser.getUid()));
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            @Override public void run() {
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                    mInterstitialAd.setAdListener(new AdListener() {
                        @Override
                        public void onAdClosed() {
                            //TODO: Intent
                            //
                            Intent intent = new Intent(getBaseContext(), MainActivity.class);
                            startActivity(intent);


                        }
                    });
                } else {
                    //TODO: Intent
                    Intent intent = new Intent(getBaseContext(), MainActivity.class);
                    startActivity(intent);



                }

            }
        }, 1000);

    }
}

【问题讨论】:

  • 卸载应用程序应该会清除所有用户凭据。
  • 感谢您的回复。但是为什么即使在重新安装应用程序后我也会得到这个日志“!!!!mCurrentUser”?你对此有什么想法吗...?
  • 不,这里没有足够的代码来说明。当前用户在全新安装时将始终为空。其他任何事情都没有意义。
  • 是的,我还认为当前用户在全新安装时将始终为空。但我不知道为什么日志会这样打印出来。我编辑了帖子以显示错误发生的位置。

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


【解决方案1】:

我猜问题是由于 firebase 权限而发生的,所以我建议您查看此链接以获取更多详细信息:

https://firebase.google.com/docs/database/security/quickstart

【讨论】:

  • 感谢您的回复,但我编辑了我的帖子。我为只有经过身份验证的用户访问数据库设置了我的规则。
猜你喜欢
  • 1970-01-01
  • 2016-09-25
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 2017-04-17
  • 2020-09-11
  • 1970-01-01
  • 2015-07-06
相关资源
最近更新 更多