Skip to content Skip to sidebar Skip to footer

Detect Firebase Auth Provider For Loged In User

How can i check that my user Loged in to my app Using which Auth Provider ? I wanted to detect that did my user loged in to my app using Facebook auth provider or using Email Provi

Solution 1:

You can always check the list of providers as Malik pointed out. However, as you can have multiple providers linked to the same user, to get the sign in method of the current User with multiple providers, you have to check the ID token. You need to check firebase.sign_in_provider claim in the token. That will give you the sign in method used to get the ID token. To get it on the client, you need to getIdToken and then parse the returned JWT with some JWT parser.

Solution 2:

FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser.getProviderData().size() > 0) {
            //Prints Out google.com for Google Sign In, prints facebook.com for Facebook
            e("TOM", "Provider: " + firebaseUser.getProviderData().get(firebaseUser.getProviderData().size() - 1).getProviderId());

        }

Solution 3:

You can use method getIdTokenResult() of your user object (firebase.User) to get IdTokenResult object, whose signInProvider property you can use to detect signin method of your logged in user.

Solution 4:

I have been puzzled over this problem and couldnt find an appropriate solution for a long time as well. The solution turns out to be short:

StringstrProvider= FirebaseAuth.getInstance().
         getAccessToken(false).getResult().getSignInProvider();

So, if (strProvider.equals("password")) then the authentication is by Email + Password, if (strProvider.equals("google.com")) then the authentication is via Google, if (strProvider.equals("facebook.com")) then the authentication is via Facebook.

Addition

However, with this one-liner you can get an exception wchich can be prevented by adding OnSuccessListener like so:

mAuth = FirebaseAuth.getInstance();
mAuth.getAccessToken(false).addOnSuccessListener(newOnSuccessListener<GetTokenResult>() {
                @OverridepublicvoidonSuccess(GetTokenResult getTokenResult) {
                    strProvider = getTokenResult.getSignInProvider();
                }
            });

Solution 5:

Reached here for same issue, unable to find the provider that user used to login. I am working on react-js app using react-firebaseui lib for readymade UI. After a lil struggle, I simply analysed the "user" object returned by auth and in that we receive an array "providerData".

Without further analysis I decided to use:

const signinProvider = user.providerData[0].providerId;

In my case, we use only 2 providers google & password. I get the user from "onAuthStateChanged" function as below:

import { fbAuth } from"./firebase";

fbAuth.onAuthStateChanged(function (user) {
    if (user) {
      console.log("authStateChanged:=====", user);
      useItFurther(user);
    } else {
      console.log("authStateChanged: no user logged in.");
      cleanUpAuthDetailsIfApplicable();
    }
  });

CAUTION: I haven't researched why is providerData an array and what more can be there in that array, what could be the sequence when there are more than 1 objects in that array, etc. In my case, we had to add a condition for email validation based on provider. Like, for a particular domain email address, force user to use a specific provider.

Post a Comment for "Detect Firebase Auth Provider For Loged In User"