import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
useLogout
A client-side hook that provides a logout function. Clears the user query cache, signs out via NextAuth, and redirects to the home page.
Source file: template/hooks/use-logout.ts
Overview
useLogout encapsulates the full logout flow for the application. It handles three concerns in order: clearing the TanStack Query cache for the current user, calling NextAuth's signOut function, and performing a hard navigation to the home page. Error handling ensures that even if the sign-out API call fails, the local cache is still cleared to prevent stale user data from persisting.
This is a client component hook (marked with "use client").
Signature
function useLogout(): {
logout: () => Promise<void>;
}
Parameters
None. This hook takes no arguments.
Return Value
| Property | Type | Description |
|---|---|---|
logout | () => Promise<void> | Async function that executes the full logout flow |
Implementation Details
The logout function performs the following steps:
- Clear user query cache -- Removes all queries matching
CURRENT_USER_QUERY_KEYfrom the TanStack Query cache. This ensures no stale user data is retained. - Sign out via NextAuth -- Calls
signOut({ redirect: false })to invalidate the session server-side without an automatic redirect. - Hard redirect -- Sets
window.location.href = "/"to navigate to the home page with a full page reload, ensuring all client state is reset.