Can't delete cookies
Unanswered
Yan - PC 01 posted this in #help-forum
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.
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,
}
}
}