首页 > 解决方案 > 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 请求的标头中使用来解决此问题。

标签: javascriptnode.jsexpresscookiesjwt

解决方案


推荐阅读