javascript - req.signedCookie 返回空对象,尽管它被正确存储
问题描述
在我将我的 AuthCookie正确保存在浏览器中之后, 我尝试从 express 中读取它(用于身份验证检查目的),但是如果我从 Postman 执行它,尽管它可以正常工作,但我得到一个空对象。
如何请求 cookie:
const formData = //grab it from form
fetch("http://localhost:3000/api/auth/login",{
method:"POST",
mode:"cors",
credentials:"include",
headers:{
"Content-Type":"application/json",
'withCredentials':true,
},
body:JSON.stringify(formData)
})
.then(res=>res.json())
.then(res=>{
console.log(res)
})
.catch(err=>console.log(err))
cookie 的设置方式:
res.cookie("AuthCookie", accessToken, {
expires: new Date(Date.now() + 900000),
httpOnly: false, //change this later
signed:true
});
我如何尝试阅读它:
const isLoggedIn = (req, res, next) => {
let token = req.signedCookies;
console.log(token);
//verifying token etc
}
在这里输出。
我试过不签名,问题完全一样。我在这里错过了什么吗?如果需要,我会发布任何其他代码:)
编辑我也通过credentials:"include"
在 GET 请求的标头中使用来解决此问题。
解决方案
推荐阅读
- docker - Nexus3 卡在初始化并且没有正确解析内容类型
- cordova - 无法让 Meteor 移动应用在 Android 模拟器上运行
- javascript - 如何从 Javascript 中的表的列创建数组?
- vue.js - 传入可变 HTML 的 Vue 插槽
- javascript - Antd:需要更改折叠体的背景颜色
- javascript - Ajax - 将表格中的内容加倍
- outlook-addin - 对于代表联系人(.vcf)的项目附件,是否会有对应的MailboxEnums.AttachmentContentFormat
- visual-studio-2015 - 尝试将数据库引用添加到引用另一个数据库的数据库得到“没有这样的源”错误
- python - python replace not na value
- azure-cosmosdb - 如何使具有 2 个分区键的文档保持同步/参照完整性?