2

I am currently using Firebase auth ui for signing in my users. What I want to achieve is to let only those users login who have created their account already. I want Firebase auth ui to disable account sign-up whenever new credentials try to log in. Please tell me how to achieve this.

This is how I sign in my users:

  if(FirebaseAuth.getInstance().getCurrentUser()==null) {
        List<AuthUI.IdpConfig> idpConfigList = Arrays.asList(
                //new AuthUI.IdpConfig.EmailBuilder().build(),
                new AuthUI.IdpConfig.GoogleBuilder().build(),
                new AuthUI.IdpConfig.PhoneBuilder().build()
        );
        startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder()
                .setAvailableProviders(idpConfigList).build(), SIGN_IN_CONST);
    }else {
        currentUser=FirebaseAuth.getInstance().getCurrentUser();
        onSignedIn();
    }

Thanks in advance.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

1 Answers1

1

What I want to achieve is to let only those users login who have created their account already.

It's the same thing if you want to check if a user is new. So to solve this, you can simply call AdditionalUserInfo's interface isNewUser() method:

Returns whether the user is new or existing

So please use the following lines of code:

mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        boolean isNewUser = task.getResult().getAdditionalUserInfo().isNewUser();
        if (isNewUser) {
            Log.d(TAG, "Is New User!"); //Restrict the authentication process
        } else {
            Log.d(TAG, "Is Old User!"); //Allow user to authenticate
        }
    }
});
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I wanted to ask if there is a method with Firebase 'Auth UI' but not the custom one. – Ashish Duhan Oct 31 '18 at 14:07
  • According to the official documentation regarding [Firebase-UI library for Android](https://github.com/firebase/FirebaseUI-Android/blob/master/auth/README.md), Firebase-UI Auth is built on top of [Firebase Auth](https://firebase.google.com/docs/auth/) and offers simple, **customizable UI bindings**. So to ahicve what you want, you should definitely use the data that comes from the authentication process, as explained in my answer above. – Alex Mamo Oct 31 '18 at 14:12
  • Is there everything alright, can I help you with other informations? – Alex Mamo Nov 01 '18 at 05:37
  • No, it was what I needed, Thanks a lot. – Ashish Duhan Nov 02 '18 at 07:23