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