Next.js Discord

Discord Forum

Next v15: await cookies.set() throws error

Answered
American black bear posted this in #help-forum
Open in Discord
Avatar
American black bearOP
I have upgraded my codebase from Next.js 14 to Next.js 15. However some of my auth logic gives me an error. I cannot debug the error because I don't understand what it means, and it does not make much sense lol.

Here is the code that gives me an error:

const sessionCookie = lucia.createSessionCookie(result.session.id) // returns the type of Cookie

(await cookies()).set(
  sessionCookie.name,
  sessionCookie.value,
  sessionCookie.attributes
)


I have also tried abstracting (await cookies()).set(...) to:

const cookieStore = await cookies()
cookieStore().set(...)


and it also does not work.

The error is attached in the image.
Image
Image
Image
Answered by American black bear
it gives me no error when writing:

cookieStore.set()
// instead of 

cookieStore().set()
View full answer

17 Replies

Avatar
does your code look off because of no ;??
Avatar
American black bearOP
I have semi disabled in prettier settings
it should not make a difference as far as I know it just looks weird because of the weird await syntax
but I have also tried this approach which fixes the syntax, but gives me the same error:

const sessionCookie = lucia.createSessionCookie(result.session.id)

const cookieStore = await cookies()
cookieStore().set(...)
Avatar
in ⬆️ which error do you get?
Avatar
American black bearOP
I think the docs are wrong
Avatar
currently I see the red underline (warning) for the whole codeblock
Avatar
American black bearOP
Image
Avatar
American black bearOP
it gives me no error when writing:

cookieStore.set()
// instead of 

cookieStore().set()
Answer
Avatar
oh man, you are right
no reason to use cookieStore()
Avatar
American black bearOP
another quick question related to this is it okay if I just define cookieStore at the top of server actions file and then use it in functions?

"use server"

import {cookies} from "next/headers"

const cookieStore = await cookies()

export async function authenticate() {
  // ...
  cookieStore.set(...)
}
@James4u
Avatar
nope, @American black bear
it should be inside your async function
Avatar
American black bearOP
okay thank you
Avatar
cookie should be inside request lifecycle