Skip to content Skip to sidebar Skip to footer

Checking User Is Password Authenticated Or With Some Provider

I have simple user and password, Facebook and Google authentication in my application. And I want to do different actions on the password Authenticated user. Code FirebaseUser us

Solution 1:

If you are using the following if statement:

if(providerId.equals("facebook.com") | providerId.equals("google.com")) {
    Toast.makeText(this,"fb or goole method is used",Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(this,"Simmple method is used",Toast.LENGTH_SHORT).show();
}

It means that if the providerId is not facebook.com or google.com it means that the user is authenticated with user and password but this statement is inccorect! In both cases, the user is authenticated with user and password. In case of facebook.com the user will be authenticated with the credentials that are coming from facebook and in case google.com the user will be authenticated with the credentials that are coming from GoogleSignInAccount object.

So what is happening if you are using that if statement, first it evaluates if the user is authenticated with user and password which obviously is since user != null and second will try to find the providerId, which can be facebook.com or google.com. That's why the order of displaying the Toast messages is Simmple method is used and second fb or goole method is used.

Edit: The simples way I can think of is to have three different authentication sections and to track which one was accessed by the user. If the user is pressing the google sign-in button then he is logged in with google and so on for the other types,


Solution 2:

I was not specifying the Provider Id for the Password Authentication. I didn't even know the provider for password authentication. It is also not present in documentation. ( as far as I know )

So what i did is:

   String providerId = profile.getProviderId();
   Toast.makeText(this,providerId,Toast.LENGTH_SHORT).show();

Put these lines in loop to get provider id of password Authentication. which is password and filtered it using elseif.

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null) {
        for (UserInfo profile : user.getProviderData()) {
            // Id of the provider (ex: google.com)
            String providerId = profile.getProviderId();
            Toast.makeText(this,providerId,Toast.LENGTH_SHORT).show();

            if(providerId.equals("facebook.com") | providerId.equals("google.com"))
            {
                Toast.makeText(this,"fb or goole method is used",Toast.LENGTH_SHORT).show();
            }
            else if(providerId.equals("password"))
            {
                Toast.makeText(this,"Simmple method is used",Toast.LENGTH_SHORT).show();

            }
        }

Post a Comment for "Checking User Is Password Authenticated Or With Some Provider"