Reputation: 7579
Following is my code to check if user is already signed in or not.
firebaseAuth = FirebaseAuth.getInstance();
currentUser = firebaseAuth.getCurrentUser();
If current user has some data, i get Token
if(currentUser != null){
String token = currentUser.getIdToken(false).getResult().getToken();
}
and here it crashes, not always, most of the times with following error message.
Method threw 'java.lang.IllegalStateException' exception.
Task is not yet complete
I am using the exact code what firebase is using in their documentation.
sometimes it also gets crashes with the same error message i shared above, when i try to sign in.
firebaseAuth.signInAnonymously()
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user != null){
String userId = user.getUid();
String token = user.getIdToken(false).getResult().getToken();
}
} else {
Log.w("", "signInAnonymously:failure", task.getException());
}
}
});
Upvotes: 0
Views: 515
Reputation: 969
You are trying to access the result directly . I have faced this issue in past and I was making the same mistake . String token = currentUser.getIdToken(false).getResult().getToken();
Instead you can try using the below code . Hope this might help
user.getIdToken(true).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
@Override
public void onSuccess(GetTokenResult result) {
String idToken = result.getToken();
//Do whatever
Log.d(TAG, "GetTokenResult result = " + idToken);
}
});
Upvotes: 5