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.
Have you tried FirebaseAuth.getInstance().getCurrentUser().getProviderId() or FirebaseAuth.getInstance().getCurrentUser().getProviders()?
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.
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).
@tikurahul Thank you for the clarification. Using getProviderData()[0] will then tell which provider the user has chosen.
problem is solved by using this -
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser user =mAuth.getCurrentUsers();
String provider = user.getProviders().get(0);
I ran into this also. Seems to be a list now, so user.getProviders().get(0) works.
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
}
To solve this problem, now you need to usegetProviderData() 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;
}
}
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馃槴
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.
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();
}
}
});
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();
// ...
}
}
}
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.
Most helpful comment
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()returnsfirebaseis 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 theFirebaseUserhas not changed).