Firebase + Flutter : get platform user used to sign in

2,987

Solution 1

This seems to be what you want: https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseUser.html#getProviderData()

In my app where I use Google logins only, I have firebaseUser.providerData[1].providerId == 'google.com'.

Btw, firebaseUser.providerData[0].providerId == 'firebase'.

I guess you could check them all and look for what providers you get for different kinds of users.

Edit: here's what I get when logging in with e-mail: https://postimg.cc/BXWGGN6h

Solution 2

Firebase has a special property providerId. But, as mentioned @GazihanAlankus it always returns firebase.

And, the property firebaseUser.providerData[1].providerId sometimes not exists (for example when user used anonymous login).

So, we should use appropriate approaches, for example:

FirebaseUser user = await _firebaseAuth.currentUser();

if (user.providerData.length < 2) {
  // do something
}
else {
  print(res.providerId);  
}

The list of values, that are returned by property providerId:

EmailAuthProviderID: password
PhoneAuthProviderID: phone
GoogleAuthProviderID: google.com
FacebookAuthProviderID: facebook.com
TwitterAuthProviderID: twitter.com
GitHubAuthProviderID: github.com
AppleAuthProviderID: apple.com
YahooAuthProviderID: yahoo.com
MicrosoftAuthProviderID: hotmail.com

I got this list from the cool research here What is the full list of provider id's for firebase.UserInfo.providerId?

Share:
2,987
Karel Debedts
Author by

Karel Debedts

Updated on December 13, 2022

Comments

  • Karel Debedts
    Karel Debedts over 1 year

    I have an app with 3 sign in methods: Google, Facebook & mail. I want to show the users that are signed in with mail a different screen. Is it possible to get the sign in method form the package firebase authentication?

    I know I can fix this by using firestore & checking if a statement is true or false. But that will cost me a read every time a user opens the app...