简体   繁体   English

如何从Firebase Admin获取Facebook ID?

[英]How to get Facebook ID from Firebase Admin?

I'm using Firebase Admin for login in my NodeJs app. 我正在使用Firebase Admin在NodeJs应用程序中登录。 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). 客户端将通过发送一个idToken(从Firebase获取)登录,然后NodeJs服务器将使用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'". 是的,我通过“ sign_in_provider:'facebook.com'”通过Facebook获得了用户的注册方法。 But I don't know how to get user's Facebook ID. 但是我不知道如何获取用户的Facebook ID。 Can someone give me an advice? 有人可以给我建议吗? So many thanks! 非常感谢!

That specific information will not be available in the ID token. 该特定信息在ID令牌中将不可用。 You will need to use the Firebase Admin SDK to lookup the user and inspect the providerData: 您将需要使用Firebase Admin SDK来查找用户并检查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);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM