Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a Firebase user is signed in using facebook authentication

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

like image 455
Taldakus Avatar asked Jul 27 '16 17:07

Taldakus


People also ask

How can I get user details of Firebase authentication?

If the user login with a custom "email/password" you don't know anything else about that user (apart from the unique user id). If a user login with Facebook, or with Google sign in, you can get other information like the profile picture url. It is explained here: firebase.google.com/docs/auth/android/… .

Is user authenticated Firebase?

Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more.


2 Answers

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");   } } 
like image 193
Frank van Puffelen Avatar answered Sep 30 '22 03:09

Frank van Puffelen


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");   }  } 
like image 23
Jayakrishnan Avatar answered Sep 30 '22 04:09

Jayakrishnan