即使用户通过身份验证,Android默认的Firebase权限也被拒绝

时间:2022-02-02 20:05:11

After user logged in and session is active, data can't be read/written due to Permission denied error. Rules are standard:

用户登录和会话激活后,由于权限被拒绝错误,数据不能被读取/写入。规则是标准:

   // These rules require authentication
{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

FirebaseAuth is working perfectly fine, and all credentials are accessible but nothing from root Firebase node.

FirebaseAuth工作得非常好,所有凭据都是可访问的,但是从根Firebase节点没有。

Instance is retrieved without any problem:

检索实例时没有任何问题:

mAuth = FirebaseAuth.getInstance();

Firebase Auth(using email and password scheme) code:

Firebase Auth(使用电子邮件和密码方案)代码:

 public void signIn(String email, String password) {
    mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (!task.isSuccessful()) {
                showAlert(Values.ERROR, Values.WRONG_EMAIL_OR_PASSWORD_ALERT);
            } else {
                changeStatus(FirebaseAuth.getInstance().getCurrentUser());
                startActivity(new Intent(mTab.getContext(), MainActivity.class));
            }
            mProgress.hide();
        }
    });
}

Before I set up a Firebase context:

在我建立一个Firebase上下文之前:

    Firebase.setAndroidContext(mTab.getContext());

And even after successful login, I can't reach my database due to a permission failure:

甚至在登录成功后,由于权限失败,我也无法到达我的数据库:

    public void changeStatus(FirebaseUser user, String status) {
    Firebase.setAndroidContext(getContext());
    mStatusRef = new Firebase(Values.FIREBASE_ADDRESS + "/statuses/" + user.getUid() + "/");
    mStatusRef.setValue(status);
    }

or

    public void setLastMessage(final MyViewHolder holder, String uid) {
    Firebase.setAndroidContext(mView.getContext());
    mMessageRef = new Firebase(Values.FIREBASE_ADDRESS + "/users/" + mUser.getUid() + "/chats/" + uid + "/message");
    mMessageRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            holder.lastMessage.setText(dataSnapshot.getValue().toString());
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    }

Error from console:

从控制台错误:

W/RepoOperation: setValue at /statuses/ghTGuwjc4saAQqyJgoMONV2Rz483      failed: FirebaseError: Permission denied
W/SyncTree: Listen at /party failed: FirebaseError: Permission denied

My JSON tree has a following structure: Firebase Database tree view from console

我的JSON树具有以下结构:控制台的Firebase数据库树视图

app/build.graddle:

应用程序/ build.graddle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "GGG.GGG.com.GGG"
    minSdkVersion 19
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
}
}
repositories {
    maven {
        url "https://jitpack.io"
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:design:23.3.0'
    compile 'com.firebase:firebase-client-android:2.5.2+'
    compile 'com.google.firebase:firebase-auth:9.2.0'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:cardview-v7:23.0.+'
    compile 'com.github.castorflex.smoothprogressbar:library:1.0.0'
    compile 'com.yalantis:phoenix:1.2.3'
    compile 'com.facebook.network.connectionclass:connectionclass:1.0.1'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.daimajia.easing:library:1.0.1@aar'
    compile 'com.daimajia.androidanimations:library:1.1.3@aar'
    compile 'com.roughike:swipe-selector:1.0.6'
    compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
}

apply plugin: 'com.google.gms.google-services'

My application consists from MainActivity and ViewPager widget. But according to Firebase Docs it should not matter as far as FirebaseAuth instance is reachable which means session is active(so user authenticated).

我的应用程序由MainActivity和ViewPager小部件组成。但是根据Firebase文档,它不应该影响到FirebaseAuth实例的可访问性,这意味着会话是活动的(因此用户身份验证)。

Maybe I'm missing something? I tried to read Firebase docs(everything is very clear and easy to understand) many times, but it feels like my problem is unique in the context.

也许我忽略了什么吗?我曾多次尝试阅读Firebase文档(一切都非常清晰、易于理解),但感觉我的问题在上下文中是独一无二的。

I changed rules to Public for development purposes only, however I can't let my database to be opened to everyone after final version released...

我只是为了开发目的而更改了规则,但是我不能让我的数据库在最终版本发布后对所有人开放……

What could be a problem?

有什么问题吗?

I'm using Firebase Console and this Android setup guide:

我使用的是Firebase控制台和Android安装指南:

Android Firebase Setup 2.5.2+

Android重火力点设置2.5.2 +

For some reason I'm not able to upgrade my graddle file to latest Firebase version 'com.google.firebase:firebase-core:9.4.0' dependency. It compiles with error "Can't resolve".

出于某种原因,我无法将渐变文件升级到最新的Firebase版本com.google.firebase: Firebase -core:9.4.0' dependency。它以“无法解决”的错误进行编译。

2 个解决方案

#1


1  

Okay, so the problem as I was expecting has been connected with a Firebase update. Please, follow the latest update [when this answer has been posted, it was 9.4.0] and migrate from old version to the newest one.

好了,我所期待的问题与Firebase的更新有关。请关注最新的更新(当这个答案被发布时,它是9.4.0)并从旧版本迁移到最新版本。

Firebase update from 'com.firebase:firebase-client-android:x.x.x' to 'com.google.firebase:firebase-database:9.4.0'

从“com.firebase:firebase-client-android:x.x重火力点更新。x”到“com.google.firebase:firebase-database:9.4.0”

If you have a problem with graddle file resolving, you should update your:

如果您对渐变文件解决有问题,您应该更新您的:

  • Android Support Repository
  • Android支持库
  • Google Play Services
  • 谷歌播放服务
  • Google Repository
  • Google存储库

Hope it will help you. Stay updated.

希望它能对你有所帮助。保持更新。

#2


0  

change the rules by these values given in image below. thats rule worked for me!!!

根据下图中给出的值更改规则。规则对我起作用了!

即使用户通过身份验证,Android默认的Firebase权限也被拒绝

#1


1  

Okay, so the problem as I was expecting has been connected with a Firebase update. Please, follow the latest update [when this answer has been posted, it was 9.4.0] and migrate from old version to the newest one.

好了,我所期待的问题与Firebase的更新有关。请关注最新的更新(当这个答案被发布时,它是9.4.0)并从旧版本迁移到最新版本。

Firebase update from 'com.firebase:firebase-client-android:x.x.x' to 'com.google.firebase:firebase-database:9.4.0'

从“com.firebase:firebase-client-android:x.x重火力点更新。x”到“com.google.firebase:firebase-database:9.4.0”

If you have a problem with graddle file resolving, you should update your:

如果您对渐变文件解决有问题,您应该更新您的:

  • Android Support Repository
  • Android支持库
  • Google Play Services
  • 谷歌播放服务
  • Google Repository
  • Google存储库

Hope it will help you. Stay updated.

希望它能对你有所帮助。保持更新。

#2


0  

change the rules by these values given in image below. thats rule worked for me!!!

根据下图中给出的值更改规则。规则对我起作用了!

即使用户通过身份验证,Android默认的Firebase权限也被拒绝