简体   繁体   中英

Android default Firebase Permission denied even after user Authenticated

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.

Instance is retrieved without any problem:

mAuth = FirebaseAuth.getInstance();

Firebase Auth(using email and password scheme) code:

 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.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

app/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).

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.

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:

Android Firebase Setup 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".

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 update from 'com.firebase:firebase-client-android:xxx' to 'com.google.firebase:firebase-database:9.4.0'

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

  • Android Support Repository
  • Google Play Services
  • Google Repository

Hope it will help you. Stay updated.

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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