0

This should set cookies in my react app I think but it is not working. i tried postman to check this API and it is most definitely setting cookies but it is not working in react

        app.post("/", (req, res) => {
          const { username } = req.body;
          const token = jwt.sign({username}, "secret", {
            expiresIn: 3 * 24 * 60 * 60,
          });
          res.status(200).cookie("token", token, { path: "/" }).send('cookie set');
        });

This is React Code

const signup = (e) => {
    e.preventDefault();
    Axios
      .post(
        "http://localhost:3001",
        {
          username: username,
          password: password,
        },
        {
          withCredentials: true,
        }
      )
      .then((res) => {
        console.log(res.data);
      });
  };

my problem is it is setting cookies but not to my react app Getting cookie from server

Code is solved

app.post("/", (req, res) => {
  const { username } = req.body;
  const token = jwt.sign({ username }, "secret", {
    expiresIn: 3 * 24 * 60 * 60,
  });
  res
    .status(200)
    .cookie("token", token, {
        sameSite: 'strict',
        path: '/',
        expires: new Date(new Date().getTime() + 100 * 1000),
        httpOnly: true,
    }).send("cookie being initialised")
});

added this in the code

Deepak
  • 3
  • 3
  • Does this answer your question? [How can I set cookie in node js using express framework?](https://stackoverflow.com/questions/16209145/how-can-i-set-cookie-in-node-js-using-express-framework) – devpolo Feb 27 '23 at 08:20
  • You can try setting the cookie manually in the browser to see if it's a problem with the cookie settings or if it's a problem with the server. You can use a tool like the Chrome DevTools to set the cookie manually. – Baki Feb 27 '23 at 08:41

0 Answers0