首页 > 解决方案 > Failed to execute 'fetch' on 'Window': Invalid value

问题描述

I'm building a login system using NodeJS, but occurer this error on console when i click on my button to login, can anyone help me to solve?, It's saying that is a invalide value on fetch.

      firebase.auth().signInWithEmailAndPassword(login, password)
        .then(({ user }) => {
          return user.getIdToken().then((idToken) => {
            return fetch("/sessionLogin", {
              method: "POST",
              headers: {
                Accept: "application/json",
                "Content-Type": "application/json",
                "CSRF-Token": Cookies.get("XSRF-TOKEN"),
              },
              body: JSON.stringify({ idToken }),
            });
          });
        })
        .then(() => {
          return firebase.auth().signOut();
        })
        .then(() => {
          window.location.assign("/profile");
        });
      return false;
    });


app.post("/sessionLogin", (req, res) => {
        const idToken = req.body.idToken.toString();
      
        const expiresIn = 60 * 60 * 24 * 5 * 1000;
      
        admin
          .auth()
          .createSessionCookie(idToken, { expiresIn })
          .then(
            (sessionCookie) => {
              const options = { maxAge: expiresIn, httpOnly: true };
              res.cookie("session", sessionCookie, options);
              res.end(JSON.stringify({ status: "success" }));
            },
            (error) => {
              res.status(401).send("UNAUTHORIZED REQUEST!");
            }
          );
      });

标签: node.jsfirebaseauthentication

解决方案


检查你的 app.js 文件 -->

app.all("*", (req, res, next) => {
    res.cookie("XSRF-TOKEN", req.csrfToken());
    next();
});

确保调用 csrfToken 函数(在第 2 行给出)。


推荐阅读