Next v15: await cookies.set() throws error
Answered
American black bear posted this in #help-forum
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:
I have also tried abstracting
and it also does not work.
The error is attached in the image.
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.
Answered by American black bear
it gives me no error when writing:
cookieStore.set()
// instead of
cookieStore().set()
17 Replies
does your code look off because of no
;
??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(...)
in ⬆️ which error do you get?
American black bearOP
I think the docs are wrong
currently I see the red underline (warning) for the whole codeblock
American black bearOP
American black bearOP
it gives me no error when writing:
cookieStore.set()
// instead of
cookieStore().set()
Answer
oh man, you are right
no reason to use cookieStore()
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
nope, @American black bear
it should be inside your async function
American black bearOP
okay thank you
cookie should be inside request lifecycle