Why Cookies from Route not setting?
Unanswered
Cape horse mackerel posted this in #help-forum
Cape horse mackerelOP
I have read the cookies can be only modified in server actions or route files.
I have created createSession function with flag 'use server' and i am calling this function in a route.ts file with cookies function provided by nextjs but its not setting the cookie.
here is the simple code https://github.com/ShourovRoy/nextjs-cookies.git
I am using nextjs 15
I have created createSession function with flag 'use server' and i am calling this function in a route.ts file with cookies function provided by nextjs but its not setting the cookie.
here is the simple code https://github.com/ShourovRoy/nextjs-cookies.git
I am using nextjs 15
8 Replies
@Cape horse mackerel I have read the cookies can be only modified in server actions or route files.
I have created createSession function with flag 'use server' and i am calling this function in a route.ts file with cookies function provided by nextjs but its not setting the cookie.
here is the simple code https://github.com/ShourovRoy/nextjs-cookies.git
I am using nextjs 15
you need to fetch the api route from the client. then the cookie can be set on the client
if you fetch the api route from the server, all it does is it tries to set the cookies on the server which doesn't work obviously
if you fetch the api route from the server, all it does is it tries to set the cookies on the server which doesn't work obviously
@joulev you need to fetch the api route from the client. then the cookie can be set on the client
if you fetch the api route from the server, all it does is it tries to set the cookies on the server which doesn't work obviously
Cape horse mackerelOP
Got it. But most of the time. I need to fetch the data server side. If I call it from the client its going to lose the ssr if i am not wrong.
@Cape horse mackerel Got it. But most of the time. I need to fetch the data server side. If I call it from the client its going to lose the ssr if i am not wrong.
during log in, you need to fetch from the client to have the cookie set in the client browser
subsequently, you can use cookies() to check whether a cookie exists <- this one you can do on the server just fine
subsequently, you can use cookies() to check whether a cookie exists <- this one you can do on the server just fine
Cape horse mackerelOP
I am trying to update the tokens use refresh tokens.
I can set cookie through login server actions, read them but they are not modifying
I can set cookie through login server actions, read them but they are not modifying
you have to call server actions from client components. calling server actions from the server has no effect because they are no longer server actions
Cape horse mackerelOP
Okay. So refreshing or updating cookies while fetching data server side not possible in nextja
Yes. You cannot set cookies from server components and that is a fundamental restriction that will hold true no matter what work around you can possibly come up with
Cape horse mackerelOP
I have solved this problem just not after days of making a structure plan to refresh it in server side. And its working now ❤️