Next.js Discord

Discord Forum

Delete cookie after checking auth state

Unanswered
Yellowfin tuna posted this in #help-forum
Open in Discord
Yellowfin tunaOP
Hello Everyone, I am working with external backend api in nextjs. Here is how.
After login, I store token in http only cookie. Then include token in auth header in subsequent requests. Then for specific user profile component, I get the current authed user which is calling external backend api. When that api is failed with 401, I want to delete the token stored inside cookie. Since I am getting the auth userState in server component, I can't delete the cookie. Should I switch into client side stuff to delete cookie after checking auth user failed.

export function Header() { 
  return <header>
    <h1>NextBlog</h1>
    
    <Suspense fallback={<Skeleton className="size-8 rounded-full" />}>
      <ProfileNav />
    </Suspense>
  </header>
}


export async function ProfileNav() {
  const user = await getCurrentUser();

  return <ProfileNavClient user={user} />;
}


export const getCurrentUser = cache(async () => {
  try {
    return await getMe(); // calling external backend api
  } catch (e) {
    if (e instanceof HTTPError && e.response.status === 401) {
      console.log("delete cookie");
    }

    return null;
  }
});

0 Replies