Check which authentication method was used by user for current session

Viewed 1426

I am making an android app using Firebase and FirebaseUI. The app allows sign in with email and password, google and facebook. How do I find which of these sign in methods was used by the user?

Describing the flow here:

  1. User sees login screen (FirebaseUI)
  2. User uses preferred sign in method and signs in
  3. Next layout is shown according to the sign-in method used by the user

For example

//this activity is launched after successful sign in
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(/*signed in with email-password*/)
    setContentView(R.layout.activity_main_email);
    else if(/*signed in with google*/)
    setContentView(R.layout.activity_main_google);
    else //signed in with facebook
    setContentView(R.layout.activity_main_facebook);
    //do something
}
2 Answers

according to this Frank van Puffelen posted that this could be achieved by inspecting the provider data of the current user.

according to his solution do it like this:

    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   //detect provider id like that

    for(UserInfo user: FirebaseAuth.getInstance().getCurrentUser().getProviderData()){

   if(user.getProviderId().equals("facebook.com")){
     //logged with Facebook

    }

    if(user.getProviderId().equals("google.com")){
     //logged with google

    }


  }

While you can get the list of providers from FirebaseAuth.getInstance().getCurrentUser().getProviderData(), a user could have multiple providers linked. So picking the first provider will always yield the same provider even when another linked provider was used to sign in. The most accurate way to determine the current provider used to sign in is by inspecting the ID token's field: firebase.sign_in_provider.

Related