在REST / GraphQL中验证Firebase Facebook令牌

时间:2018-08-19 14:04:10

标签: node.js firebase firebase-authentication graphql facebook-login

我关注了这份文档https://firebase.google.com/docs/auth/web/facebook-login

令牌是客户端提供的新的accessToken

const context = ({req}) => {
      const credential = firebase.auth.FacebookAuthProvider.credential(token)

  try {
    firebase.auth().signInAndRetrieveDataWithCredential(credential)
  } catch(error) {
    console.error(error)
  }
}

但是我收到此错误(100%确保正确复制了令牌)

{ [Error: Your API key is invalid, please check you have copied it correctly.]
  code: 'auth/invalid-api-key',
  message:
   'Your API key is invalid, please check you have copied it correctly.' }
{ _extensionStack:
   GraphQLExtensionStack { extensions: [ [CacheControlExtension] ] } }

我做错了吗?

1 个答案:

答案 0 :(得分:0)

找到了解决方案!

在客户端,我必须通过以下方式将facebook accessToken更改为IdToken:

getIdToken = async () => {
    const provider = new firebase.auth.FacebookAuthProvider()
    const result = await firebase.auth().signInWithPopup(provider)

    const IdToken = await firebase.auth().currentUser.getIdToken(true)
}

然后验证后端中的IdToken

try {
    const decodedToken = await admin.auth().verifyIdToken(token)
    console.log(decodedToken)
  } catch (error) {
    // error code
  }