响应本地Google登录-Firebase无效令牌

时间:2019-08-28 11:33:33

标签: firebase react-native authentication google-cloud-functions google-authentication

已修复: 因此,我从react-native-google-signin收到的令牌还不够,我还应该安装Firebase并对其进行身份验证(如接受的答案所示),然后使用来自firebase的令牌。

原始帖子:

我正在使用react-native-google-signin登录用户到应用程序,登录后我得到了jwt令牌。

在firebase上,我具有以下https云功能:

exports.verify = functions.https.onRequest((req, res) => {
  const tokenId = req.get('Authorization').split('Bearer ')[1];
  return admin.auth().verifyIdToken(tokenId)
    .then((decoded) => res.status(200).send(decoded))
    .catch((err) => res.status(401).send(err));
});

当我从react-native-google-signin发送令牌进行验证时,收到以下响应:

{
    "code": "auth/argument-error",
    "message": "Firebase ID token has incorrect \"aud\" (audience) claim. Expected \"my-project\" but got \"1232345345-sdfsdfsdfsdf.apps.googleusercontent.com\". Make sure the ID token comes from the same Firebase project as the service account used to authenticate this SDK. See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token."
}

在这种情况下,aud是来自“我的项目”的身份验证方法的Firebase Google登录的Web客户端ID。 我将如何解决此问题,因为它是Firebase给我令牌的,而Firebase却拒绝了令牌...

1 个答案:

答案 0 :(得分:1)

首先,请确保用于托管您的云功能的Firebase项目与React应用程序中使用的Firebase项目相同。

第二,确保在React应用程序中,您正在检索Firebase用户,然后调用getIdToken

示例

import { GoogleSignin } from 'react-native-google-signin';

onLoginOrRegister = () => {
  GoogleSignin.signIn()
    .then((data) => {
      const credential = firebase.auth.GoogleAuthProvider.credential(data.idToken, data.accessToken);
      return firebase.auth().signInWithCredential(credential);
    })
    .then((user) => {

      // ** Now that the user is signed in, you can get the ID Token. **

      user.getIdToken(/* forceRefresh */ true).then(function(idToken) ... 

    })
    .catch((error) => {
      const { code, message } = error;
    });
}
firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken)