【问题标题】:Cookies not showing upCookie 未显示
【发布时间】:2021-10-19 15:18:04
【问题描述】:

我无法让我的 express 服务器在登录时附加 cookie。 Bellow 是我编写的代码,我最近在 Vercel 上部署了我的 react 应用程序,它正在向我仍在本地运行的 express 服务器发出请求。当我在本地运行 react 应用程序时,一切正常,并且附加了 cookie。

谢谢,

快递服务器

router.post("/", (req, res) =>{
      const email = req.body.userName.toLowerCase()
      const pass = req.body.password
      db.query(`SELECT * FROM users WHERE email = '${email}'`, async (err, result) => {
        if(result.rows.length === 1){
          let user = result.rows[0]
          bcrypt.compare(pass, user.password, (err, result) => {
            if (err) throw err;
            if(result === true){
              user.password = 'blocked'
              let token = jwt.sign({"tokenInfo": user, "userType": user.user_type}, secret, { algorithm: 'HS256'})
              return res.cookie('userId', token, {
                maxAge: 60000 * 60 * 2,
                httpOnly: false
              }).send({'message': "User Loged In", 'userType': user.user_type, 'userID': user.apaid})
            } else {
              return res.send({'message': 'Invalid Password', 'userType': 'false'})
            }
          })
        } else if(result.rows.length > 1) {
          res.send('WTF')
        } else {
          res.send({'message': 'Invalid User Name', 'userType': 'false'})
        }
        console.log(res.cookies)
      })
    })

【问题讨论】:

    标签: reactjs express cookies vercel


    【解决方案1】:

    您的应用的前端和后端部分应在同一个域中提供,以便使用 cookie。如果您使用 create-react-app 创建应用程序,则可以在您的反应应用程序的 package.json 文件中设置代理。

      "devDependencies": {
        ...
      },
      "proxy": "http://*address of your locally running server eg:localhost:5000/*" 
    

    【讨论】:

    • 我向localhost:3500 添加了一个代理,这是我的服务器正在运行和重新部署的地方,但我仍然没有收到任何cookie。我正在运行 nodemon 以在编辑时重新启动服务器。
    猜你喜欢
    • 2020-09-03
    • 2018-04-27
    • 2016-09-02
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多