Next.js Discord

Discord Forum

Cookies dont save...

Answered
Ray posted this in #help-forum
Open in Discord
RayOP
Can someone explain me why the cookies dont save in the server...

Login File (login/router.ts): https://pastebin.com/AqpggCGH
In this file, i use cookieStore.getAll(), and return the cookies when i write the cookies

But in another file, like the header, the cookieStore.getAll(), only return []
Answered by ᴉuɐpɹɐɐ
When you set cookies in the server you arent setting the cookies in the server but instead you are providing instruction to save these cookies in the client via the Set-Cookie Header.

This is how cookies works fundamentally. Cookies are never set on the server. So when you try to .getAll() its still returning cookies from the initial request.
View full answer

2 Replies

@Ray Can someone explain me why the cookies dont save in the server... Login File (login/router.ts): https://pastebin.com/AqpggCGH In this file, i use `cookieStore.getAll()`, and return the cookies when i write the cookies But in another file, like the header, the `cookieStore.getAll()`, only return `[]`
When you set cookies in the server you arent setting the cookies in the server but instead you are providing instruction to save these cookies in the client via the Set-Cookie Header.

This is how cookies works fundamentally. Cookies are never set on the server. So when you try to .getAll() its still returning cookies from the initial request.
Answer
It needs to traverse to the client in order for it to be 'set'