Next.js Discord

Discord Forum

Is there really no way to set cookie during page load?

Unanswered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
As the title says, I'm trying to set a cookie during page load. On the server side, I have an auth function that checks my current cookies (token + refresh_token). When the token expires, I want to use the refresh token to generate a new one and set them as new cookies.

How the hell do you achieve that? Is it even possible? I know cookies can only be set via server actions or route handlers, so I tried calling a route handler inside my auth function. Doesn’t work—probably because it's a server-to-server call. Then, I tried doing it client-side, but you can't set an HTTP cookie like that either.

Maybe middleware? Nope. I'm using gRPC to get my new token, so that's out.

Can it be done? Do I need some crazy redirect setup? Even then, I’d have to return to the original path... and guess what? You don't have access to the current URL on the server side.

Any ideas?

31 Replies

Komondor
you can set a cookie using the cookies method server side

  cookies().set('name', 'lee')
  // or
  cookies().set('name', 'lee', { secure: true })
  // or
  cookies().set({
    name: 'name',
    value: 'lee',
    httpOnly: true,
    path: '/',
  })
you can also use this method in an API handler. So you can set up an API route that will receive your refresh token, and then set a cookie containing the new token
you cant set cookie in page.tsx or layout.tsx, however in middleware you can. you can also run a route handler or server action in the page to set cookie (but its a different request)
Masai LionOP
the problem with middleware is that i am using jose lib to veryify more secure jwt, and ofc middleware dont support nodejs api
such a bollock
Asian black bear
There are alternative libs that are Edge-compatible and work within the middleware for that purpose.
+1 to what near said, but if you are talking about [jose](https://www.npmjs.com/package/jose) from npm, it has worked in my edge runtime tests so should work. Are you using a more obscure part of the lib, or have you just not tested edge support?
Masai LionOP
i am using EdDSA so its require a file, i assume thats what causing the probklem
@Asian black bear could you point me into direction for this "node middleware" option? Some good lib?
Asian black bear
I didn't say anything about a Node middleware. Only that there are JWT libs for Edge.
You might be able to check if you can pass the file contents for keys instead of the file itself then you could store the key file as a server-side env variable.
as near said just directly import your key with a env without using the filesystem from node
thos does include EdDSA
this*
Masai LionOP
yeah, i think ive made it work with key as string
still not quite there, stuggling ..... it seems middlewaer is not called on every page navigation :/
like its beeing cached?
omg ...... setting cookies in middleware DOES NOT set them for server actions, they still use the old coookie
that is .........
like kill me
on next request its there already
.....
ehhhhh
Masai LionOP
ok, it looks like the combination of putting the key as string + new canary version seems to solve the problem, thanks anyone here that helped me
so rly middleware check jwt, if invalid, calls an api route handler that returns new tokens, and set them in response
the server actions (IN CANARY ONLY) already have the new cookie, so they can use it to access resources