Vinayakaram Nagarajan
Vinayakaram Nagarajan

Reputation: 111

How to determine which provider the user logged in with

I am using multiple Firebase sign in methods in my app (Facebook, Google, Phone, etc). But once the user is logged in, I am unable to determine which provider was used to sign in.

I tried below(very close to the recommended solution. But does not work): _auth.currentUser().providerData[0].providerId

I expect it to return something like "google.com" or "facebook.com" Instead it returns "firebase"

I cant find any other solution where it clearly lets us determine if a google or facebook sign in was used. All solutions in the forum iOS specific or for web. Can't find anything flutter specific. Appreciate any help in advance.

Upvotes: 4

Views: 1511

Answers (3)

Youcef Megoura
Youcef Megoura

Reputation: 153

You can use the following code :

@override
  Future<void> signOut() async {
    final FirebaseAuth _auth = FirebaseAuth.instance;
    try {
      final _providerData = _auth.currentUser!.providerData;
      if (_providerData.isNotEmpty) {
        if (_providerData[0]
            .providerId
            .toLowerCase()
            .contains('google')) {
          await GoogleSignIn().signOut(); // google signOut
        } else if (_providerData[0]
            .providerId
            .toLowerCase()
            .contains('facebook')) {
          await FacebookAuth.instance.login(); // facebook signOut
        }
      }
      await _auth.signOut(); // firebase signOut
    } catch (e) {
      print(e);
    }
  }

Upvotes: 2

Vinayakaram Nagarajan
Vinayakaram Nagarajan

Reputation: 111

I checked and _auth.currentUser().providerData[0].providerId does provide "google.com" or "facebook.com"

Not sure at what point it showed me "firebase". Will post to this if I figure this out

Upvotes: 2

Anthony Sette
Anthony Sette

Reputation: 837

I am not sure about facebook since I have never implemented login with facebook, but for Google, you may be able to do something like this.

You probably have a statement similar to GoogleSignInAccount googleUser = await _googleSignIn.signIn(). Why not check if google User is null or not once the user is signed in or on auth change? This will let you know if the user signed in with google.

Similar functionality for facebook should be possible but I just don't know if it is the same.

If googleUser != null then google was used. If say facebookUser != null then facebook was used, Then if googleUser is null and say facebookUser is null it was email.

Maybe this is helpful, maybe not, but it is just what I could think of right now.

Upvotes: 0

Related Questions