简体   繁体   English

无法读取未定义的属性“代码”,解码 Firebase ID 令牌失败

[英]Cannot read property 'code' of undefined, Decoding Firebase ID token failed

I intermittently get this error on about 5% of requests, and I am not sure why.我在大约 5% 的请求中间歇性地收到此错误,我不知道为什么。 It seems to work most of the time, but I'd like to get it to 100%.它似乎在大部分时间都有效,但我想将其提高到 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)

client客户

const authLink = setContext(async (_, { headers }) => {
    let token;
    if (firebase.auth().currentUser) {
        token = await firebase.auth().currentUser.getIdToken();
    }
    return {
        headers: {
            ...headers,
            authorization: `Bearer ${token}`,
        },
    };
});

server服务器

            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);

Your client code will result an authorization header of Bearer undefined if the firebase.auth().currentUser is falsey.如果 firebase.auth firebase.auth().currentUser为 falsey,您的客户端代码将导致Bearer undefined的授权标头。 This authorization header will not cause no token to be logged in the server code (its length is 16).这个授权头不会导致no token记录在服务器代码中(其长度为 16)。 The server code will then fail when running firebase.auth().verifyIdToken('undefined') .服务器代码将在运行firebase.auth().verifyIdToken('undefined')时失败。

You need to either prevent the client from sending the request if firebase.auth().currentUser is falsey or catch the undefined token in the server code.如果 firebase.auth firebase.auth().currentUser为 falsey,则您需要阻止客户端发送请求,或者在服务器代码中捕获未定义的令牌。

Note: using an undefined variable in a template string results in the string 'undefined' (not an empty string).注意:在模板字符串中使用未定义的变量会导致字符串“未定义”(不是空字符串)。

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

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