如何知道使用Google登录的用户是否是新用户?

时间:2018-01-09 19:44:56

标签: android firebase firebase-authentication

我需要知道刚刚登录Google的用户是否是新用户,因此我可以在我的应用中为新用户采取必要的操作。 我听说过这个方法isNewUser(),但我实际上并不熟悉抽象方法,我不确切知道如何在我的情况下使用它: https://firebase.google.com/docs/reference/android/com/google/firebase/auth/AdditionalUserInfo.html#isNewUser()

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mFirebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "signInWithCredential:success");
                        // TAKE ACTION IF IT IS A NEW USER.
                    } else {
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        Toast.makeText(MainActivity.this, R.string.authentication_failed,
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });

提前致谢!

2 个答案:

答案 0 :(得分:1)

您最好自己定义成为&#34;新用户&#34;的意义,并使用数据库来反映这一点。例如,如果用户的数据库中没有任何数据由其UID键入,则可以假设它们是新的。然后更新数据库以反映它们不再是新的事实。

答案 1 :(得分:0)

现在有更好的方法可以做到这一点。 要在登录后检查用户是新用户还是现有用户,请使用isNewUserhttps://firebase.google.com/docs/reference/android/com/google/firebase/auth/AdditionalUserInfo#isNewUser() 它位于AdditionalUserInfo上,当任务结算时可在AuthResult上使用。