简体   繁体   English

Firebase错误:权限被拒绝。 无法从Firebase数据库读取/写入

[英]Firebase error: Permission denied. Unable to read/write from Firebase Database

I need to give read/write access only to authenticated users, but it seems like Firebase is not recognizing that the user is authenticated. 我只需要对经过身份验证的用户授予读/写访问权限,但是Firebase似乎无法识别该用户已通过身份验证。

After I sign in the user with email and password, I am assuming user is authenticated and should be allowed to read/write from database. 使用电子邮件和密码登录用户后,我假设用户已通过身份验证,应被允许从数据库读取/写入。 However permission is denied. 但是,权限被拒绝。

Here is the code for sign in activity: 这是登录活动的代码:

 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();
            }

        }
    });

I want to use the default rule in firebase that allows only auth users to read/write from database: 我想在Firebase中使用默认规则,该规则仅允许auth用户从数据库读取/写入:

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

But when I change the rules as below my code is able to get read/write access to database. 但是,当我如下更改规则时,我的代码能够获得对数据库的读/写访问权限。

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

This is fine in testing, however I cannot continue with production with this method. 这在测试中很好,但是我无法继续使用此方法进行生产。 Please help, how to authenticate a registered user to access database in Firebase. 请帮助,如何对注册用户进行身份验证以访问Firebase中的数据库。

Your code is a mixture of API calls from the legacy 2.xx SDK, for example: 您的代码混合了来自旧版2.xx SDK的API调用,例如:

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

and the new 9.xx SDK: 和新的9.xx SDK:

firebaseAuth.signInWithEmailAndPassword()

The two SDKs are not compatible. 这两个SDK不兼容。 You need to use or the other. 您需要使用或其他。 The Upgrade Guide explains how to migrate from the legacy SDK to the new SDK. 升级指南介绍了如何从旧版SDK迁移到新SDK。

You also need to use the Firebase console that corresponds to the SDK used: Legacy Console or new Firebase Console . 您还需要使用与使用的SDK相对应的Firebase控制台: 旧版控制台或新的Firebase控制台

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM