检查用户是否在 Android 中的 Firebase Google 身份验证中首次通过身份验证
问题描述:
我在 Android 应用程序中使用 Firebase 身份验证,并使用 Google 帐户身份验证作为登录应用程序的选项.
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?
答
要检查是否是用户第一次登录,只需调用 OnCompleteListener 中的
回调.AdditionalUserInfo.isNewUser()
方法.onComplete
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"));
}
}
};
查看文档以获取更多参考AdditionalUserInfo
Check the docs for more reference AdditionalUserInfo