Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AuthUI: Method to get provider id #329

Closed
xsorifc28 opened this issue Sep 26, 2016 · 14 comments
Closed

AuthUI: Method to get provider id #329

xsorifc28 opened this issue Sep 26, 2016 · 14 comments
Labels

Comments

@xsorifc28
Copy link

Is there a method to get which provider was used to authenticate?

I'm trying to get additional information about the user from their facebook/google profile but cannot seem to find a method which returns the provider used to authenticate.

This will be a useful feature.

@SUPERCILEX
Copy link
Collaborator

Have you tried FirebaseAuth.getInstance().getCurrentUser().getProviderId() or FirebaseAuth.getInstance().getCurrentUser().getProviders()?

@asciimike asciimike added the auth label Sep 26, 2016
@xsorifc28
Copy link
Author

FirebaseAuth.getInstance().getCurrentUser().getProviderId() always return firebase.

As for getProviders(), it will return all the providers that were previously authenticated. Potentially the list can contain facebook.com, google.com.

I was looking specifically for a method which returns what provider was used to sign in for that instance.

I could use the existing providers as a workaround, checking if facebook exists and updating user info via facebook, and same for google - but I'd rather use the provider that the user has chosen to login with at the time.

So, I think it would be helpful if AuthUI could store which provider was used to login with.

@tikurahul
Copy link

The getProviders() method list is sorted by the most recent provider used to sign in. So the first element in getProviderData() is the method that the user used to sign in.

Also the reason why FirebaseAuth.getInstance().getCurrentUser().getProviderId() returns firebase is because a backing Firebase Account is always created irrespective of how the user signs in. This helps with linking and unlinking of accounts where users may want to attach more than one credential (however your view of the FirebaseUser has not changed).

@xsorifc28
Copy link
Author

@tikurahul Thank you for the clarification. Using getProviderData()[0] will then tell which provider the user has chosen.

@akshaysahai19
Copy link

akshaysahai19 commented Jan 22, 2017

problem is solved by using this -

FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser user =mAuth.getCurrentUsers();

String provider = user.getProviders().get(0);

@grennis
Copy link

grennis commented Jun 2, 2017

I ran into this also. Seems to be a list now, so user.getProviders().get(0) works.

@SagarKisanAvhad
Copy link

my user is logged in by facebook then also getProviderData().get(0) returning com.google.firebase

so I used mAuth.getCurrentUser().getProviders().get(0)
this returns current provider. if user logged in by google then above function returns google.com
if user is logged in by facebook then this function returns facebook.com

so simply strore returning value in string variable(let say provider) and add condition like below:
if(provider.contains("google")){

//add logic for google provider

}else if(provider.contains("facebook")){

//add logic for google provider

}

@gastsail
Copy link

To solve this problem, now you need to use getProviderData() wich is a list

you can find it in this docs : https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseUser.html#getProviderData()

Returns a List of UserInfo objects that represents the linked identities of the user using different authentication providers that may be linked to their account. Use this to access, for example, your user's basic profile information retrieved from Facebook whether or not the user used Facebook Login to sign in to the current session.

Snippet

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    List<? extends UserInfo> infos = user.getProviderData();
    for (UserInfo ui : infos) {
        if (ui.getProviderId().equals(GoogleAuthProvider.PROVIDER_ID)) {
            return true;
        }
    }

@takhmina0907
Copy link

takhmina0907 commented Jul 29, 2018

I have a problem with Firebase Auth 5.0. Value of type 'AuthDataResult' has no member 'providerID'
this is my code:
func registerUser (withEmail email: String, andPassword password: String, userCreationComplete: @escaping (_ status: Bool, _ error: Error?) -> ()){ Auth.auth().createUser(withEmail: email, password: password) { (user, error) in guard let user = user else{ userCreationComplete(false, error) return } let userData = ["provider": user.providerID, "email": user.email] DataService.instance.createDBUser(uid: user.uid, userData: userData) userCreationComplete(true, nil) } }
Please heeeelp😫

@swapnilgt
Copy link

All the above solutions (the one mentioned by @grennis and the one mentioned by @tikurahul) don't seem to work any longer. I am using firebase auth 16.0.3 and irrespective of the method used for login, I get the list of providers in the same order (first facebook.com followed by password). Not sure if I am understood something wrong here.

@moguzalp
Copy link

moguzalp commented Sep 27, 2018

It seems above answer is deprecated recent solution is:

As noted here

var uiConfig = {
        callbacks: {
          signInSuccessWithAuthResult: function(authResult, redirectUrl) {
            var providerId = authResult.additionalUserInfo.providerId;
            //...
          },
          //..
       }

and for display in page

firebase.auth().onAuthStateChanged(function (user) {
      if (user) {
        user.getIdToken().then(function (idToken) {
          
          $('#user').text(welcomeName + "(" + localStorage.getItem("firebaseProviderId")+ ")");
          $('#logged-in').show();
		}
	}
});

@sorrat
Copy link

sorrat commented Nov 21, 2019

It is possible to get used provider from IdpResponse:

Start UI:

Intent authUIIntent = AuthUI.getInstance().createSignInIntentBuilder().build();
getCurrentActivity().startActivityForResult(authUIIntent, RC_SIGN_IN);

And listen for result:

private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
    @Override
    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
        if (requestCode == RC_SIGN_IN) {
            IdpResponse response = IdpResponse.fromResultIntent(data);
            String providerType = response.getProviderType();
            // ...
        }
    }
}

@danielfaust
Copy link

danielfaust commented Dec 11, 2019

The following returns the currently used provider ("google.com", "password", ...) as a string.

FirebaseAuth.getInstance().getCurrentUser().getIdToken(false).getResult().getSignInProvider()

where the false in getIdToken(false) means to not refresh the token but to use the cached one.

dpa99c added a commit to dpa99c/cordova-plugin-firebasex that referenced this issue Apr 29, 2020
dpa99c pushed a commit to dpa99c/cordova-plugin-firebasex that referenced this issue May 25, 2020
duncan-c pushed a commit to howbout-ltd/cordova-plugin-firebasex that referenced this issue May 27, 2020
duncan-c pushed a commit to howbout-ltd/cordova-plugin-firebasex that referenced this issue May 27, 2020
dpa99c added a commit to dpa99c/cordova-plugin-firebasex that referenced this issue Jun 1, 2020
dpa99c pushed a commit to dpa99c/cordova-plugin-firebasex that referenced this issue Jun 1, 2020
@arshadnzr
Copy link

The following returns the currently used provider ("google.com", "password", ...) as a string.

FirebaseAuth.getInstance().getCurrentUser().getIdToken(false).getResult().getSignInProvider()

where the false in getIdToken(false) means to not refresh the token but to use the cached one.

This didnt work in POCO M2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests