Next.js Discord

Discord Forum

Can't delete cookies

Unanswered
Yan - PC 01 posted this in #help-forum
Open in Discord
If I receive a certain code from the backend response I need to delete the token in cookies, but it always gives me this error:" Cookies can only be modified in a Server Action or Route Handler. Read more:"
I've tried to use Server Actions and Route Handlers, but Route Handlers doesent seem to be getting the cookies context of the browser or something (they dont appear on the logs, it shows that the cookies are emptys), and Server Actions give me the same error that I cant modify the cookies.

"use server"
import { isRedirectError } from "next/dist/client/components/redirect-error"
import { API_BASE_URL } from "./api-config"
import { AUTH_ERROR_CODES } from "./error-codes"
import { getAccessToken } from "./session"
export interface TypedFetchResponse<T> {
  error_code: number | null
  message: string | null
  data: T | null
  success: boolean
}

export async function typedFetch<T>(
  path: string,
  options: RequestInit = {},
): Promise<TypedFetchResponse<T>> {
  const url = `${API_BASE_URL}${path}`
  const fetchOptions = {
    ...options,
    cache: "no-store" as RequestCache,
  }
  try {
    const token = await getAccessToken()
    if (token) {
      fetchOptions.headers = {
        ...fetchOptions.headers,
        Authorization: `Bearer ${token}`,
        Cookie: `access_token=${token}`,
      }
    }
    const response = await fetch(url, fetchOptions)
    const result = (await response.json()) as TypedFetchResponse<T>
    if(result.error_code === 1005) {
     console.log("remove <:fine:753870958363803719> cookie")
    }
    return result
  } catch (error) {
    // Re-throw redirects do Next.js (não são erros reais)
    if (isRedirectError(error)) {
      throw error
    }

    console.error(`Erro ao buscar ${path}:`, error)
    return {
      error_code: AUTH_ERROR_CODES.SERVER_ERROR,
      message: "Erro interno do servidor",
      data: null,
      success: false,
    }
  }
}

8 Replies

@Yan - PC 01 If I receive a certain code from the backend response I need to delete the token in cookies, but it always gives me this error:" Cookies can only be modified in a Server Action or Route Handler. Read more:" I've tried to use Server Actions and Route Handlers, but Route Handlers doesent seem to be getting the cookies context of the browser or something (they dont appear on the logs, it shows that the cookies are emptys), and Server Actions give me the same error that I cant modify the cookies. "use server" import { isRedirectError } from "next/dist/client/components/redirect-error" import { API_BASE_URL } from "./api-config" import { AUTH_ERROR_CODES } from "./error-codes" import { getAccessToken } from "./session" export interface TypedFetchResponse<T> { error_code: number | null message: string | null data: T | null success: boolean } export async function typedFetch<T>( path: string, options: RequestInit = {}, ): Promise<TypedFetchResponse<T>> { const url = `${API_BASE_URL}${path}` const fetchOptions = { ...options, cache: "no-store" as RequestCache, } try { const token = await getAccessToken() if (token) { fetchOptions.headers = { ...fetchOptions.headers, Authorization: `Bearer ${token}`, Cookie: `access_token=${token}`, } } const response = await fetch(url, fetchOptions) const result = (await response.json()) as TypedFetchResponse<T> if(result.error_code === 1005) { console.log("remove <:fine:753870958363803719> cookie") } return result } catch (error) { // Re-throw redirects do Next.js (não são erros reais) if (isRedirectError(error)) { throw error } console.error(`Erro ao buscar ${path}:`, error) return { error_code: AUTH_ERROR_CODES.SERVER_ERROR, message: "Erro interno do servidor", data: null, success: false, } } }
Black imported fire ant
Can you share your getAccessToken function?
@Black imported fire ant Can you share your getAccessToken function?
export async function getAccessToken(): Promise<string | undefined> {
const cookieStore = await cookies()
return cookieStore.get("access_token")?.value
}
@Yan - PC 01 If I receive a certain code from the backend response I need to delete the token in cookies, but it always gives me this error:" Cookies can only be modified in a Server Action or Route Handler. Read more:" I've tried to use Server Actions and Route Handlers, but Route Handlers doesent seem to be getting the cookies context of the browser or something (they dont appear on the logs, it shows that the cookies are emptys), and Server Actions give me the same error that I cant modify the cookies. "use server" import { isRedirectError } from "next/dist/client/components/redirect-error" import { API_BASE_URL } from "./api-config" import { AUTH_ERROR_CODES } from "./error-codes" import { getAccessToken } from "./session" export interface TypedFetchResponse<T> { error_code: number | null message: string | null data: T | null success: boolean } export async function typedFetch<T>( path: string, options: RequestInit = {}, ): Promise<TypedFetchResponse<T>> { const url = `${API_BASE_URL}${path}` const fetchOptions = { ...options, cache: "no-store" as RequestCache, } try { const token = await getAccessToken() if (token) { fetchOptions.headers = { ...fetchOptions.headers, Authorization: `Bearer ${token}`, Cookie: `access_token=${token}`, } } const response = await fetch(url, fetchOptions) const result = (await response.json()) as TypedFetchResponse<T> if(result.error_code === 1005) { console.log("remove <:fine:753870958363803719> cookie") } return result } catch (error) { // Re-throw redirects do Next.js (não são erros reais) if (isRedirectError(error)) { throw error } console.error(`Erro ao buscar ${path}:`, error) return { error_code: AUTH_ERROR_CODES.SERVER_ERROR, message: "Erro interno do servidor", data: null, success: false, } } }
Black imported fire ant
Do you wanna update browser cookie in server-side component?
👀
@Black imported fire ant Do you wanna update browser cookie in server-side component?
Ive thought i could update them in server side functions
I know I can set them via server actions when the user interact with something, I thought i could do the same with server side functions