0

I have this code in node js API :

const jwt = require("jsonwebtoken");

generateToken = (user, res) => {
  const token = jwt.sign(user, process.env.ACCESS_TOKEN_SECRET, {
    expiresIn: "1800s",
  });

  res
    .cookie("token", token, {
      httpOnly: true,
    })
    .status(200)
    .json({ message: "Logged in successfully  " });
};

module.exports = generateToken;

I have this code in Next js project :

const onSubmitLogin = (data) => {
  
    axios
      .post(
        `http://localhost:8000/login`,
        {
          email: data.email,
          password: data.password,
        },
        {
          headers: {
            "Content-Type": "application/json; charset=UTF-8",
          },
        }
      )
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });
   
  };

If I use Postman, i get the cookie with the token.

But, when I use the browser I dont get the cookie stored in cookies. I tried to add withCredentials: true, in axios request but nothing changes.

However, I get the message "Logged in successfully " in the browser's console

  • Did you try to add `credentials: 'include'`? – Nick Vu Mar 28 '22 at 11:05
  • @Nick Vu Yes, it's not working ^^ –  Mar 28 '22 at 11:10
  • hmm I cannot run your code locally, but I found this https://stackoverflow.com/questions/36824106/express-doesnt-set-a-cookie somebody also raised the same problem. I think you cm find some useful info there – Nick Vu Mar 28 '22 at 12:38

0 Answers0