Next.js Discord

Discord Forum

modifying cookies in nextjs

Unanswered
Boreal Chickadee posted this in #help-forum
Open in Discord
Boreal ChickadeeOP
Context -
we are building project where we summaries user emails
we are using next-auth(AuthJs) for authentication ,because we are accessing sensitive scopes like read user email or reply to email , google sent us access token which expires in 1hr , To get new access token google sent refresh token which we are storing in our DB
we are doing this real time , as soon as user land on dashboard , we get user emails and summarize them and show on dashboard


Problem-
When user hit dashboard route , i want to check if access token is expired or not and set cookies but not able to do it nextjs

Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options

2 Replies

Thrianta
Im guessing you have a function that handles this logic and it is getting called in the body of a server component, such as the page.tsx.

How bad would it be to put that logic in a route handler and make a normal fetch request from the page (or even from middleware on that route)?
English Angora
when a client requests a page it is delieverd to them via streaming prodocole which does not allow the server to alter the cookies [check out the good to know section](https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options). The only way you can do it is via a route handler or a server actions (which is affectively a route handler)