FireBaseUI Auth - how to know if account is from a new signup or existing user?

断了今生、忘了曾经 提交于 2021-02-14 18:40:53

问题


I am using firebaseUI for authentication. it essentially opens a a external activity and logs the user into firebase and sends the developer a call back in onActivityResult. It works great the problem is i need to know if the user is a new signup or an existing user. is there any kind of metadata or something i can use to know this ? here is what i have so far IN 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);
                   }
               });

   }

and then for the result:

    @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

            }
        }
    }
}

I see a meta class that maybe can help me but i dont know how to use it.

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


回答1:


Do not use the creation and sign in timestamp comparison. I found it to be unreliable.

IdpResponse has a isNewUser() method to tell you whether the login is a new account or not.




回答2:


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

At the time of writing, Looks like each logged in user has meta data as i suspected. we can check the last sign time to know if its a new account. I heard they will be making this easier in the future, check later versions of firebase authentication before attempting this.



来源:https://stackoverflow.com/questions/49347535/firebaseui-auth-how-to-know-if-account-is-from-a-new-signup-or-existing-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!