node.js - 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!");
}
);
});
解决方案
检查你的 app.js 文件 -->
app.all("*", (req, res, next) => {
res.cookie("XSRF-TOKEN", req.csrfToken());
next();
});
确保调用 csrfToken 函数(在第 2 行给出)。
推荐阅读
- android - 如何使用转换为 LiveData 对象设置多个属性?
- python - Python 切换器无法访问
- php - 在 PHP 中上传多个文件失败
- mysql - mysql中的Sample-Gene-Expression数据存储问题
- oauth-2.0 - OAuthTAI 身份验证失败,状态 = 401,WWW-Authenticate: Bearer realm="imfAuthentication", scope="UserAuthRealm" in MFP 版本 7.1
- ibm-mobilefirst - MFP8 - 从数据库中删除已部署的适配器
- php - 如何在phpword中设置页面内容对齐或页面大小?
- sql - 将 60 列表从一个存储过程返回到另一个
- r - 当数字不完全匹配时匹配 2 个数据框中的列
- google-cloud-platform - 可以将私人报告嵌入网站并过滤非 G 用户对数据的访问