I'm logging in my users anonymously if they are not logged in, but the onAuthStateChanged callback is not being triggered. Additionally, whenever I try to use mAuth.getCurrentUser().getUid()
, it gives me a NullPointerException. Here is my code:
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// do something
} else {
mAuth.signInAnonymously().addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
System.out.println("SIGNED IN ANONYMOUSLY");
System.out.println(mAuth.getCurrentUser().getUid()); // crashes here
}
});
}
}
};
Been stuck on this for hours and I have no idea what the issue could be.
Thanks in advance for any help.
I am pretty much sure its because your signInAnonymously
call is not returning successful in onComplete
callback. Replace
System.out.println("SIGNED IN ANONYMOUSLY");
System.out.println(mAuth.getCurrentUser().getUid());
with
if (task.isSuccessful()) {
System.out.println("SIGNED IN ANONYMOUSLY");
System.out.println(mAuth.getCurrentUser().getUid());
}
else{
Log.i(TAG, "signInAnonymously", task.getException());
}
and check the exception
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.