Check if user is authenticated for the first time in Firebase Google Authentication in Android

rainman picture rainman · Sep 17, 2016 · Viewed 29k times · Source

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?

Answer

EricT picture EricT · Feb 6, 2018

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