I'm trying to find out what provider my user is using, and I have no idea how! the language that I use is vanilla javascript.
There's a very similar question:
Firebase Auth - get provider ID
but this only works in swift 3...
As the linked answer and Hareesh said, you will need to loop over the providerData
array since a user can be signed in with multiple providers. So:
user.providerData.forEach(function(providerData) {
console.log("Signed in with "+providerData.providerId);
});
For a working example see: https://jsbin.com/vuzafuq/edit?html,js,output (check the JavaScript console for the actual providers)
I have tried multiple providers.
If you want to get all providers user have logged in, you can use user.providerData
.
But the order of providerData is not based on the desc order by user log in time which the Swift SDK claimed.
This doesn't apply to Javascript SDK.
If you want to get current provider, you can decode the idToken which contain current logged in provider sign_in_provider
.
The format would be something like this
{
.....
"email": ....,
"email_verified": true,
"firebase": {
"identities": {
"google.com": [
....
],
"email": [
....
]
},
"sign_in_provider": "password"
}
}
You could tried to decode by https://jwt.io/ or using npm package jsonwebtoken
to decode.
Test on the firebase 7.15.0
and hope this works for you.
Firebase provides the currently signed-in provider information inside the user object like below (I have used the AngularFire Framework Library for the below example).
For the web platform, please refer to the following code:
import { AngularFireAuth } from '@angular/fire/auth';
import 'firebase/auth';
export class AuthenticationService {
constructor(private auth: AngularFireAuth) { }
getCurrentUser(){
this.auth.onAuthStateChanged((user) => {
if (user) {
// User is signed in.
user.getIdTokenResult()
.then((idTokenResult) => {
let currentProvider = idTokenResult.signInProvider;
// The result will look like 'google.com', 'facebook.com', ...
});
} else {
// No user is signed in.
}
});
}
}
For more details, check out the official doc here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With