#How can i delete cookies from server component

1 messages · Page 1 of 1 (latest)

twilit basin
#

hey guys i was trying to delete cookie from server but it's seem to be impossible i tried

const cookiesToDelete = await clearAdvancedTokenCookies({
cookies: {
get: (name) => cookieStore.get(name),
getAll: () => cookieStore.getAll(),
},
});
console.log(cookiesToDelete)
deleteAuthCookies()

'use server';
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
import { clearAdvancedTokenCookies } from '@/lib/authTokenManager';

export async function deleteAuthCookies() {
const cookieStore = cookies();
const cookiesToDelete = await clearAdvancedTokenCookies({
cookies: {
get: (name) => cookieStore.get(name),
getAll: () => cookieStore.getAll(),
},
});
cookiesToDelete.forEach(name => cookieStore.delete(name));
redirect('/login');
}

this and other several method but it shows

⨯ unhandledRejection: Error: Cookies can only be modified in a Server Action or Route Handler.

i also tried to understand what actually https://nextjs.org/docs/app/api-reference/functions/cookies#options this server action or route handler reffer but can't understand can someone please help me with this issue?

API Reference for the cookies function.

alpine ridgeBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

dusty apex
#

The error is clear:

Cookies can only be modified in a Server Action or Route Handler.
You cannot manipulate cookies in server components, only in a server action or route handler that is being invoked from the client.