How to get Facebook ID from Firebase Admin?

流过昼夜 提交于 2020-01-06 06:00:13

问题


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!


回答1:


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);
  });


来源:https://stackoverflow.com/questions/50406027/how-to-get-facebook-id-from-firebase-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!