Cookies can only be modified in a Server Action or Route Handler
Unanswered
Ant posted this in #help-forum
AntOP
I am trying to use signOut that is exported from auth.ts and I get this message
[ Server ] Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#options
[ Server ] Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#options
5 Replies
@Ant I am trying to use signOut that is exported from auth.ts and I get this message
[ Server ] Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#options
Argente Brun
cant really help much with ur question without u providing additional context, or code snippets
But it COULD be cuz u don't have a "use server" at the top of ur auth.ts file
But it COULD be cuz u don't have a "use server" at the top of ur auth.ts file
@Ant I am trying to use signOut that is exported from auth.ts and I get this message
[ Server ] Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#options
Authentication libraries store the user token in the cookies, when you “signOut()” you’re effectively deleting the cookie via “cookies.delete()” which is an operation that can only happen in Server Actions or Route Handlers, that’s because these two have access to the requests headers.
Request Headers allow you to operate with cookies via the “HTTP Cookie” header, sending or modifying information on the client side (cookies are only stored on the client side)
Request Headers allow you to operate with cookies via the “HTTP Cookie” header, sending or modifying information on the client side (cookies are only stored on the client side)
Idk if I understood your question correctly, a little more context would help
In your case, I would create a folder with actions for auth: actions/auth
“use server”;
export async function signIn(){ … }
export async function signOut(){
….
signOut()
// or whatever way your
// auth library handles this
…
}
“use server”;
export async function signIn(){ … }
export async function signOut(){
….
signOut()
// or whatever way your
// auth library handles this
…
}
@Ant did it work?