2

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
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Amit Ghosh
  • 63
  • 6

1 Answers1

1

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
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    Thanks, It worked like heaven but is using for loop a great idea in Onstart as it can produce a significant delay on my activity to start – Amit Ghosh Apr 12 '20 at 08:15
  • 1
    That loop it's a simple loop, should not produce a significant delay. Maybe there are other elements in the code that produce that. – Alex Mamo Apr 12 '20 at 11:36
  • In my case FirebaseAuth.getInstance().getCurrentUser() is null the user has logged out from firebase. How can i check if is registered in the database? – cousma Apr 15 '20 at 14:51
  • @cousma In that case you need an [auth listener](https://stackoverflow.com/questions/50885891/one-time-login-in-app-firebaseauth). – Alex Mamo Apr 15 '20 at 15:50
  • @AlexMamo The authListener checks if the FirebaseAuth.getInstance().getCurrentUser() is null and if it is, it directs the user to loginActivity and if it's not directs to MainActivity. What i need is when the user has logged out from firebase in th app (i do it by FirebaseAuth.getInstance().signOut() ) and tries to use the app again i want to check if the user has registered with Firebase - if they are registered i want to login them by typing their password but if not i want to make a phone authentication with OTP, so show them an OtpView and do the phone authentication. How to check that? – cousma Apr 15 '20 at 16:44
  • @cousma You cannot do that. You cannot authenticate a user, only the user does. – Alex Mamo Apr 15 '20 at 16:55
  • @AlexMamo I don't want to authenticate the user, i want to check if the user exists in the database that's all. if it exists i don't want to do phone authentication again this should happen only once when they register, if the user exists in the database i want to just type their password and login. But the problem is how will i check if the user is phone registered in Firebase. – cousma Apr 15 '20 at 17:00
  • @cousma Add user data in Firebase Realtime Database or Cloud Firestore and check that before doing some action. – Alex Mamo Apr 15 '20 at 17:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211737/discussion-between-cousma-and-alex-mamo). – cousma Apr 15 '20 at 17:27
  • @AlexMamo Could you elaborate a bit more please? I am adding data in the Realtime Database. This is an example of an entry in my realtime database Auto-Generated ID +16505553434: "some@email.com" email: "some@email.com" first name: "First name" last name: "Last name" phone: "+16505553434" – cousma Apr 15 '20 at 18:22
  • @cousma These are questions that are not related to this one. Add a new question so we can take a look at it. – Alex Mamo Apr 15 '20 at 18:30
  • @AlexMamo https://stackoverflow.com/questions/61236899/how-to-check-when-a-user-has-phone-authenticated-in-firebase – cousma Apr 15 '20 at 19:19