Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 自动刷新firebase身份验证而不重新加载窗口?_Node.js_Reactjs_Firebase_Firebase Authentication - Fatal编程技术网

Node.js 自动刷新firebase身份验证而不重新加载窗口?

Node.js 自动刷新firebase身份验证而不重新加载窗口?,node.js,reactjs,firebase,firebase-authentication,Node.js,Reactjs,Firebase,Firebase Authentication,我试图找出为什么我的JWT一直过期,而刷新似乎不起作用,我的代码处理这一问题的关键是: React.useEffect(() => { firebase.auth().onAuthStateChanged(async (auth) => { if (auth) { const token = await auth.getIdToken(); setToken(token) nookies.

我试图找出为什么我的JWT一直过期,而刷新似乎不起作用,我的代码处理这一问题的关键是:

React.useEffect(() => {
    firebase.auth().onAuthStateChanged(async (auth) => {
        if (auth) {
            const token = await auth.getIdToken();
            setToken(token)
            nookies.destroy({}, "token");
            nookies.set({}, "token", token, {
                path: "/"
            });

            setAuth(auth);

            const val = jwt_decode(token) as any;
            const exp = Number(val.exp);

            let timeToExpiry = moment(exp * 1000 - 1000 * 5).diff(moment());
            const updateToken = () => {
                setTimeout(async () => {
                    console.log("REFRESHING TOKEN");
                    await firebase.auth().currentUser.getIdToken(true).then(c => {
                        setToken(c);
                        const val = jwt_decode(c) as any;
                        const exp = Number(val.exp);
                        timeToExpiry = moment(exp * 1000 - 1000 * 5).diff(moment());
                        //alert("refreshing "  + timeToExpiry);
                        //console.log(timeToExpiry + " DONE REFRESHING TOKEN");
                        updateToken();
                    });
                }, timeToExpiry)
            }
            updateToken();
        }
        else {
            setToken("");
            nookies.destroy({}, "token");
            setAuth(null);

            var provider = new firebase.auth.GoogleAuthProvider();
            provider.setCustomParameters({
                prompt: 'select_account'
            });

            firebase.auth().signInWithRedirect(provider);
        }
    })
}, [])
超时起作用,它在到期之前调用getIdToken(),但是下一个请求仍然出错,JWT token已过期,需要我刷新窗口,从而实际修复它


有没有办法让这些令牌在不刷新窗口的情况下进行刷新?

只有在用户的身份验证状态发生更改时才会触发onAuthStateChanged,因此在用户登录或注销时才会触发

如果要获取ID令牌的更新,请收听