简体   繁体   中英

Use Anonymous Login in Firebase to Auth a user on Firestore

I'm a bit lost, I'm writing an android app that will give new users who are not yet signed-in to read some data from firestore , so I set up anonymous login and I want to authenticate the anonymous user using firebase-auth , once the user is authenticated I want to use his uid/cred to get access to the firestore database .

Firebase auth:

mAuth = FirebaseAuth.getInstance();
        mAuth.signInAnonymously()
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {

                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        printWithDash("INSIDE onComplete");

                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            printWithDash("SIGN IN SUCCESSFULL");
                            FirebaseUser user = mAuth.getCurrentUser();
                            printWithDash(user.toString()); // address
                        } else {
                            // If sign in fails, display a message to the user.
                            printWithDash("SIGN IN FAIL!!!");
                        }
                    }
                });

And here's how I write to firestore:

 db.collection("answers")
                .add(jsonAnswers)
                .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                    @Override
                    public void onSuccess(DocumentReference documentReference) {
                        System.out.println("-----------------------------------------------------------------------------------------");
                        System.out.println("DocumentSnapshot added with ID: " + documentReference.getId());
                        System.out.println("-----------------------------------------------------------------------------------------");


                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        System.out.println("Error adding document " + e);
                    }
                });

here are my firetore rules

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
        allow read: if request.auth.uid != null && request.auth.token.firebase.sign_in_provider == 'anonymous';
        allow write: if request.auth.uid != null && request.auth.token.firebase.sign_in_provider == 'anonymous';
    }
  }
}

I'm new to android development, how does authentication works on android apps?. And how can I use my anonymous user to write to firestore db ?

Put only the request.auth.uid;= null; to able read and write the signed in anonymous users:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

A users who sign in anonymously are also authenticated.

I've learned that signing anonymously will save your user cred on the phone itself, look at the device file explorer under apps/ your app. so once we did the anonymous login, the user is considered authenticated until you will do mAuth.signOut();

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