javaandroidfirebasefirebase-authenticationonstart

How to check for a phone auth user in Android?


How to modify this onStart() method to get my separate Phone auth user database?

@Override
protected void onStart() {
    super.onStart();
    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
    if (firebaseAuth.getCurrentUser() != null) {
     if(what condition required to check for phone auth){
        startActivity(new Intent(EnterAs.this, UI_Main_User.class));
        finish();
    } else {
       for email auth users
    }
}

Solution

  • You can do that by calling UserInfo's getProviderData() method on the FirebaseUser object, to get the list of all authentication providers that are used by the authenticated user. One more thing to note is that each UserInfo class contains a method named getProviderId() which returns the ID of the provider.

    A workable solution in code might look like this:

    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    List<? extends UserInfo> userInfos = firebaseUser.getProviderData();
    for (UserInfo userInfo : userInfos) {
        String providerId = userInfo.getProviderId();
        if (providerId.equals(PhoneAuthProvider.PROVIDER_ID)) {
            //Your logic goes here
        }
    }
    

    If in the future you'll use for example, Google authentication with Firebase, then you should check against:

    GoogleAuthProvider.PROVIDER_ID