How to determine if a Firebase user is signed in using facebook authentication
Solution 1
In version 3.x and later a single user can be signed in with multiple providers. So there is no longer the concept of a single provider ID. In fact when you call:
FirebaseAuth.getInstance().getCurrentUser().getProviderId()
It will always return firebase
.
To detect if the user was signed in with Facebook, you will have to inspect the provider data:
for (UserInfo user: FirebaseAuth.getInstance().getCurrentUser().getProviderData()) {
if (user.getProviderId().equals("facebook.com")) {
System.out.println("User is signed in with Facebook");
}
}
Solution 2
Sharing for FirebaseAuth
targeting version 6.x.x
(Swift 5.0), year 2020:
I use Auth.auth().currentUser?.providerData.first?.providerID
.
This will return password if logged in via email. And facebook.com if via Facebook.
Solution 3
For me, the following solution is working. First, get the firebase user object if you have'nt already:
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser firebaseUser = mAuth.getCurrentUser();
Now use the following on the FirebaseUser object to get the sign in provider:
firebaseUser.getIdToken(false).getResult().getSignInProvider()
Sources:
https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseUser
https://firebase.google.com/docs/reference/android/com/google/firebase/auth/GetTokenResult.html
It will return password, google.com, facebook.com and twitter.com for email, google, facebook and twitter respectively.
Solution 4
In my app, I use Anonymous Firebase accounts. When I connect Firebase auth with a Facebook account or Google Account I am checking like the following:
for (UserInfo user: FirebaseAuth.getInstance().getCurrentUser().getProviderData()) {
if (user.getProviderId().equals("facebook.com")) {
//For linked facebook account
Log.d("xx_xx_provider_info", "User is signed in with Facebook");
} else if (user.getProviderId().equals("google.com")) {
//For linked Google account
Log.d("xx_xx_provider_info", "User is signed in with Google");
}
}
Solution 5
There exist information in the responding Intent.
Refer to following snippet:
The responseCode is either "phone", "google.com", "facebook.com", or "twitter.com".
`import com.firebase.ui.auth.AuthUI; import com.firebase.ui.auth.IdpResponse; ..... @Override protected void onActivityResult(final int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RC_SIGN_IN) { progressBar.setVisibility(View.VISIBLE); IdpResponse response = IdpResponse.fromResultIntent(data); if (resultCode == RESULT_OK) { String providerCode = response.getProviderType(); ... } }
Related videos on Youtube
Taldakus
Updated on June 06, 2022Comments
-
Taldakus about 2 years
I am using firebase from google and I have some trouble with user authentication. After logging with facebook I obtain FirebaseUser in AuthStateListener, but how can I detect if this user is logged via facebook or differently?
UPDATE As @Frank van Puffelen said FirebaseAuth.getInstance().getCurrentUser().getProviderId() should return "facebook", but in my case it returns "firebase". Now I cannot figure out what's the reason of this behavior. When I got FacebookToken I do something like this:
AuthCredential credential = FacebookAuthProvider.getCredential(facebookToken.getToken()); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { } } });
And afterthat before onComplete() method is called, my AuthStateListener gets user which provider id is not "facebook" as it should be. Am I doing something wrong? I followed official google documentation
-
Craig C. almost 8 yearsI believe that FirebaseUser.getProviderId() will return "facebook.com" but I cannot test it as my app does not use facebook. Maybe give that a try if you could?
-
Taldakus almost 8 yearsLook at my updated question please.
-
-
Taldakus almost 8 yearsUnfortunately getProviderId() in my case returns "firebase". I will update my question, would be great if you could take a look.
-
Frank van Puffelen almost 8 yearsThe code in your update does precisely what the first half of my answer explains. Did you use the loop I have in my second snippet? The first
UserInfo
will indeed have provider idfirebase
(as it is for the merged user). But the second one in my tests hadgoogle.com
(I used the Google provider). -
Taldakus almost 8 yearsOf course you are right! I made simple mistake... Thanks a lot man.
-
Erik over 7 years@FrankvanPuffelen how can the user be signed in with multiple providers, how does FireBase know that my google and my facebook when they are both singed into a particular FireBase backend, are the same user?
-
kbpontius almost 6 years@FrankvanPuffelen Is this documented somewhere we could reference the other Provider Ids? I can't seem to find it.
-
rednuht over 5 years@kbpontius - From what I could gather: facebook - "facebook.com", email - "password", github - "github.com", google - "google.com", phone - "phone", playgames - "playgames.google.com", twitter - "twitter.com". Anonymous login seems to return "firebase", but I was unable to find any documentation confirming that.