Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

android - Firebase Authentication only works after closing and reopening app

I'm building an Android app that queries a Firebase Cloud Firestore database, and for doing this, the user must be authenticated because of this rule:

match /{document=**} {
    allow read: if request.auth != null;
}

However, after authenticating with Google inside of the app, a query is executed and fails giving the insufficient permissions message:

D/: Query failed: PERMISSION_DENIED: Missing or insufficient permissions.

Here's the code:

    private fun firebaseAuthenticateGoogle(account: GoogleSignInAccount?) {
    try {
        val credential = GoogleAuthProvider.getCredential(account?.idToken, null)

        mAuth.signInWithCredential(credential).addOnSuccessListener { i ->
            val firestore = FirebaseFirestore.getInstance(mAuth.app)
            firestore.collection("units").document("1").get()
                .addOnSuccessListener {
                    Log.d(TAG, "Query was successful")
                }
                .addOnFailureListener { e ->
                    Log.d(TAG, "Query failed: " + e.message)
                }
        }
    }

If I close the app and reopen it, it works fine, as if the user were only authenticated after closing the app. However, if I check if mAuth.currentuser == null it returns false, so the user should be authenticated right after clicking the Sign In button...

question from:https://stackoverflow.com/questions/65626629/firebase-authentication-only-works-after-closing-and-reopening-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...