Before:
<form action="/signup" method="POST">
server.post("/signup", () => {
const sessionID = crypto.randomUUID()
return {
status: 302,
headers: {
"Location": "/",
"Set-Cookie": `session=${sessionID}`
}
}
})
The cookie is successfully set in browser.
After changing the form route from /signup to /form/signup:
<form action="/form/signup" method="POST">
server.post("/form/signup", () => {
const sessionID = crypto.randomUUID()
return {
status: 302,
headers: {
"Location": "/",
"Set-Cookie": `session=${sessionID}`
}
}
})
When the request is handled cookie is now not set. In chrome dev tools there’s one entry in the cookie bar flashes for 0.5s and deletes itself. The flash also exists before, but the cookie eventually stayed. Why would changing route break the cookie behavior?
I’m using the http package in https://bun.sh/
>Solution :
You haven’t included a Path attribute in the cookie.
The first attempt is for the URL /signup so the default path is /.
The second attempt is for the URL /form/signup so the default path is /form/.
A cookie belonging to /form/ will not appear when you visit /.