Skip to Content

signOut

Signs the user out in the UI. Clears the stored token and user, then tells every useSession to re-render.

Usage

app/components/logout-button.tsx
import { Button } from "antd"; import { useNavigate } from "react-router-dom"; import { signOut } from "app/hooks"; export function LogoutButton() { const nav = useNavigate(); return <Button onClick={() => { signOut(); nav("/login"); }}>Sign out</Button>; }

Signature

app/hooks.ts
function signOut(): void;

Parameters

None.

Returns

Nothing. After the call:

  • calls through useApiClient() and useApi() no longer send an Authorization header;
  • getUser returns null;
  • every component using useSession re-renders with user set to null.

signOut doesn’t navigate. Call nav("/login") yourself if you need to.

Examples

The API answers 401 when the session has expired or the account is locked. Sign out and go to the sign-in page:

app/components/api-error.tsx
import { signOut } from "app/hooks"; export function handleApiError(e: any, message: { error: (s: string) => void }, toLogin: () => void) { if (e?.response?.status === 401) { signOut(); toLogin(); return; } message.error(e?.response?.data?.message ?? "Something went wrong"); }

Next steps

Last updated