我约有5%的请求间歇性地收到此错误,但我不确定为什么。似乎大多数时候都可以使用,但我想将其提高到100%。
{"name":"myapp","hostname":"worker-844ddfbc9f-ntlmz","pid":18,"level":50,
"err":"[Throws: Cannot read property 'code' of undefined]",
"msg":"Context creation failed: Decoding Firebase ID token failed. Make sure you passed the entire string JWT which represents an ID token. See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.",
"time":"2020-02-12T02:16:33.538Z","v":0}
TypeError: Cannot read property 'code' of undefined
at FirebaseAuthError.get [as code] (/app/node_modules/firebase-admin/lib/utils/error.js:51:35)
at FirebaseAuthError.FirebaseError.toJSON (/app/node_modules/firebase-admin/lib/utils/error.js:67:24)
at JSON.stringify (<anonymous>)
at prettyJSONStringify (/app/node_modules/apollo-server-core/dist/runHttpQuery.js:257:17)
at throwHttpGraphQLError (/app/node_modules/apollo-server-core/dist/runHttpQuery.js:26:42)
at Object.<anonymous> (/app/node_modules/apollo-server-core/dist/runHttpQuery.js:66:28)
at Generator.next (<anonymous>)
at fulfilled (/app/node_modules/apollo-server-core/dist/runHttpQuery.js:4:58)
at process._tickCallback (internal/process/next_tick.js:68:7)
客户
const authLink = setContext(async (_, { headers }) => {
let token;
if (firebase.auth().currentUser) {
token = await firebase.auth().currentUser.getIdToken();
}
return {
headers: {
...headers,
authorization: `Bearer ${token}`,
},
};
});
服务器
let token = req.headers.authorization;
if (!token || !token.length) {
console.log('no token');
}
token = token.split('Bearer ')[1];
// validate JWT and pluck user id
const { uid } = await firebase.auth().verifyIdToken(token);
// find the user based on id
const user = await firebase.auth().getUser(uid);
您的客户端代码将生成授权标头,Bearer undefined
如果标头firebase.auth().currentUser
为false。该授权标头将不会导致no token
登录到服务器代码中(其长度为16)。服务器代码将在运行时失败firebase.auth().verifyIdToken('undefined')
。
您需要阻止客户端发送请求(如果firebase.auth().currentUser
错误),或者捕获服务器代码中的未定义令牌。
注意:在模板字符串中使用未定义的变量会导致字符串“未定义”(不是空字符串)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句