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

后端 未结 8 1456
一向
一向 2020-11-30 06:16

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

8条回答
  •  盖世英雄少女心
    2020-11-30 06:47

    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 completeListener = new OnCompleteListener() {
            @Override
            public void onComplete(@NonNull Task 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

提交回复
热议问题