检查用户是否在 Android 中的 Firebase Google 身份验证中首次通过身份验证 [英] Check if user is authenticated for the first time in Firebase Google Authentication in Android

查看:24
本文介绍了检查用户是否在 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

这篇关于检查用户是否在 Android 中的 Firebase Google 身份验证中首次通过身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆