FireBaseUI身份验证 - 如何知道帐户是来自新注册还是现有用户?

时间:2018-03-18 11:52:07

标签: android firebase-authentication firebaseui

我正在使用firebaseUI for authentication。它本质上打开一个外部活动,并将用户登录到firebase,并在onActivityResult中向开发人员发送回调。它工作得很好问题是我需要知道用户是新注册还是现有用户。是否有任何类型的元数据或我可以用来知道这个?这是我到目前为止在JAVA ANDROID:

    private void ititFireBaseUi() {

       AuthUI.getInstance()
               .signOut(getActivity())
               .addOnCompleteListener(new OnCompleteListener<Void>() {
                   public void onComplete(@NonNull Task<Void> task) {
                       // Choose authentication providers
                       List<AuthUI.IdpConfig> providers = Arrays.asList(
                               new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
                               new AuthUI.IdpConfig.Builder(AuthUI.PHONE_VERIFICATION_PROVIDER).build(),
                               new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build(),
                               new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build());
                       //new AuthUI.IdpConfig.Builder(AuthUI.TWITTER_PROVIDER).build());

// Create and launch sign-in intent
                       startActivityForResult(
                               AuthUI.getInstance()
                                       .createSignInIntentBuilder()
                                       .setAvailableProviders(providers)
                                       .setLogo(R.drawable.logo)
                                       .build(),
                               RC_SIGN_IN);
                   }
               });

   }

然后结果:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_SIGN_IN) {
            IdpResponse response = IdpResponse.fromResultIntent(data);
//I WOULD LIKE TO KNOW HERE IF THE USER IS A NEW USER OR EXISTING USER
            String msg = "";
            if (resultCode == RESULT_OK) {
                // Successfully signed in
                FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                msg = "generating token with email:" + user.getEmail();

                Timber.d(msg);
                Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();

                presenter.generateTokenWithFireBase(user);
                // ...
            } else {
                // Sign in failed, check response for error code

            }
        }
    }
}

我看到meta class可能对我有所帮助,但我不知道如何使用它。

gradle:implementation'com.firebaseui:firebase-ui-auth:3.2.2'

2 个答案:

答案 0 :(得分:3)

public boolean isNewSignUp(){
        FirebaseUserMetadata metadata = mAuth.getCurrentUser().getMetadata();
        return metadata.getCreationTimestamp() == metadata.getLastSignInTimestamp();
    }

看起来每个登录用户都有我怀疑的元数据。我们可以检查最后的签到时间,以了解它是否是一个新帐户。

答案 1 :(得分:0)

请勿使用创建和登录时间戳比较。我发现它不可靠。

IdpResponse有一种isNewUser()方法来告诉您登录名是否是新帐户。