I am using Firebase Authentication in an Android application, and I am using Google account authentication as an option to sign in to the application.
How can I know if the user is signed in to the application for the first time or not?
To check if it's the first time user logs in, simply call the AdditionalUserInfo.isNewUser()
method in the OnCompleteListener.onComplete
callback.
Example code below, be sure to check for null.
OnCompleteListener<AuthResult> completeListener = new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
boolean isNew = task.getResult().getAdditionalUserInfo().isNewUser();
Log.d("MyTAG", "onComplete: " + (isNew ? "new user" : "old user"));
}
}
};
Check the docs for more reference AdditionalUserInfo