我正在尝试验证 idToken 后端。用户已成功登录到 Firebase 客户端,但是当我尝试在后端验证 idToken 时,我收到这个不是很有帮助的错误消息
Firebase Auth ID token has incorrect "aud" claim
错误消息似乎变得更加有用,归结为在 auth 键中没有项目名称:
Error: Firebase ID token has incorrect "aud" (audience) claim. Expected "stripmall-0000" but got "617699194096-0aafcvsml0gke61d6077kkark051f3e1.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/server/verify-id-tokens for details on how to retrieve an ID token.
任何人都知道可能出了什么问题?我从客户端正确收到了 tokenId,所以这应该不是问题。如果以前有人问过这个问题或以任何其他方式微不足道,请真诚地道歉。
firebase.initializeApp({
serviceAccount: {
"type": "service_account",
"project_id": <project id here>,
"private_key_id": <key id goes here>,
"private_key": <key goes here>
"client_email": <email goes here>,
"client_id": <my client id>,
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": <url goes here>
},
databaseURL: <my db url here>
});
router.post("/verify", function (req, res) {
firebase.auth().verifyIdToken(req.body.idToken).then(function (decodedToken) {
var uid = decodedToken.sub;
res.send(uid);
}).catch(function (error, param2) {
console.log(error); // 'Firebase Auth ID token has incorrect "aud" claim'
});
});
最佳答案
您的问题可能是您尝试使用由 auth()
函数之一返回的 JWT token ,例如 firebaseRef.auth().signInWithPopup()
。这些确实返回了一个 JWT token ,但是身份验证声明可能是错误的,并且不会通过 verifyIdToken
的验证。 Firebase 技术支持证实了这一点。
您必须使用 firebaseRef.auth().currentUser.getToken()
函数。该 token 将通过验证。
关于node.js - Firebase Auth ID token 的 "aud"声明不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38335127/