Express doesn't set a cookie

后端 未结 10 1598
花落未央
花落未央 2020-12-14 01:45

I have problem with setting a cookies via express. I\'m using Este.js dev stack and I try to set a cookie in API auth /login route. Here is the cod

10条回答
  •  情深已故
    2020-12-14 02:17

    I had the same issue with cross origin requests, here is how I fixed it. You need to specifically tell browser to allow credentials. With axios, you can specify it to allow credentials on every request like axios.defaults.withCredentials = true however this will be blocked by CORS policy and you need to specify credentials is true on your api like

    const corsOptions = {
        credentials: true,
        ///..other options
      };
    
    app.use(cors(corsOptions));
    

    Update: this only work on localhost For detail answer on issues in production environment, see my answer here

提交回复
热议问题