Next.js Discord

Discord Forum

Setting a Cookie on the server

Answered
Spectacled Caiman posted this in #help-forum
Open in Discord
Spectacled CaimanOP
In my app, after a user verifies their identity I set a jwt encoded string on their browser. At least that's the plan.

I tried doing
response.cookies.set({
  name: "ACCESS_TOKEN",
  value: signature,
  httpOnly: true,
  maxAge: 60000 * 60 * 24 * 7,
});


and then

redirect('/dashboard');


My issue is:
The cookie doesn't get set. I get no error messages, no nothing. I even got redirected but no cookies were set.

8 Replies

Answer
@Marchy https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options
Spectacled CaimanOP
I do this on the client?
@Spectacled Caiman I do this on the client?
@Marchy Click to see attachment
Spectacled CaimanOP
oh.... thanks :)
@Spectacled Caiman oh.... thanks :)
It's functionally the same as doing this

  return new Response('Hello, Next.js!', {
    status: 200,
    headers: { 'Set-Cookie': `token=${token.value}` },
  })
But you can use it from server actions and server components
Spectacled CaimanOP
It works! Thanks 💙