Reputation: 81
I'm currently working on a chat app. The frontend is built with react and is deployed on vercel, and the backend is built with express and is on heroku. So the app is currently in production mode.
On the login page, when the user provides their credentials (email and password), a request gets sent to the /login route on the backend where the credentials the user provided are checked to see if they're correct. Simple enough. If the credentials are not correct, a 500 status code is sent back to the client, but if they are correct, 2 cookies are created, and they're sent back to the client along with a 200 status code.
Here's where I'm struggling. When the user provides the right information, the 2 cookies get created on the backend and are sent to the client. I know this because I can see the 2 cookies in the Cookies tab of the chrome developer tools. But when I try to output the value of the cookies using document.cookie, it comes out as an empty string.
Frontend:
const response = await fetch(url, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: JSON.stringify({email: email, password: password})
})
Backend:
res.cookie("Cookie1", value1, {sameSite: "none", secure: true, httpOnly: false})
res.cookie("Cookie2", value2, {sameSite: "none", secure: true, httpOnly: true})
Server:
const cors = require('cors')
const corsOptions = { origin: originUrl, credentials: true, methods: ['GET', 'POST'] }
app.use(cors(corsOptions))
// I made sure to declare the above before declaring the routes
Additional Info:
Please let me know if you there's anything else you'd like to know. Thanks in advance.
Upvotes: 3
Views: 1672
Reputation: 702
I don't have much knowledge of frontend , but to access cookies on frontend, you have to use proxy. Cookies can only be access on same port or server.
See this link, hope this might help you!
Upvotes: 1