Hung
Hung

Reputation: 89

How to get Facebook ID from Firebase Admin?

I'm using Firebase Admin for login in my NodeJs app. Client will login by sending an idToken (which is get from Firebase), and then NodeJs server will get the user by using admin.auth().verifyIdToken(idToken).

The result will look like this:

{ iss: 'https://securetoken.google.com/myapp-5cde1',
  name: 'Manh Hung',
  picture: 'https://graph.facebook.com/185960225411xxx/picture',
  aud: 'xxxxxx-5cde1',
  auth_time: 1526626xxx,
  user_id: 'uCxxxxxxxxxxxxxxxQR4d2',
  sub: 'uCwNoxxxxxxxxxxxxxxuQR4d2',
  iat: 15266xxx1,
  exp: 15266xxx91,
  firebase:
   { identities: { 'facebook.com': [Array] },
     sign_in_provider: 'facebook.com' },
  uid: 'uCxxxxxxxxxxxxxxxQR4d2' 
}

Yes, I got the user's registration method is by Facebook via "sign_in_provider: 'facebook.com'". But I don't know how to get user's Facebook ID. Can someone give me an advice? So many thanks!

Upvotes: 0

Views: 2640

Answers (1)

bojeil
bojeil

Reputation: 30808

That specific information will not be available in the ID token. You will need to use the Firebase Admin SDK to lookup the user and inspect the providerData:

admin.auth().getUser(uid)
  .then(function(userRecord) {
    // Assuming the first provider linked is facebook.
    // The facebook ID can be obtained as follows.
    console.log(userRecord.providerData[0].uid);
  })
  .catch(function(error) {
    console.log("Error fetching user data:", error);
  });

Upvotes: 5

Related Questions