简体   繁体   中英

Creating anonymous user in Firebase doesn't work

I have been trying to solve this problem for more than 4 hours now, still nothing...

I want that only signed in users can write and read data to my database, so I decided to create anonymous user. I set firebase rules like this:

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

Now I have this code in my MainActivity:

mAuth = FirebaseAuth.getInstance();
        mAuth.signInAnonymously().addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    System.out.print("NEW USER SIGNED IN?");
                } else {
                    Log.d(TAG, "Fail");
                }
            }
        });

The purpose of this code is to create an anonymous user, which can read and write into my database, but this code isn't even getting invoked.

Is this the correct way to create an anonymous user which may read and write data into my firebase database?

If you have followed these steps: https://firebase.google.com/docs/auth/android/anonymous-auth

Added Firebase to your project, added the dependency and enabled the anonymous auth (which you do in the firebase auth console), this code should work. If you use an emulator, make sure it has internet connection.

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