Logout Button
A copy-paste React logout control for Knock Codes access screens — clears the shared session on click. Zero dependencies.
Best used for Any screen sharing a Session Provider session that needs a manual way to lock again.
Requires a <KnockCodesProvider> ancestor — a logout action only makes sense where a session is already being
shared with something else that reads it.
Minimal setup
<KnockCodesProvider expectedHash={hash}>
<KnockCodes>
<Dashboard />
<LogoutButton />
</KnockCodes>
</KnockCodesProvider>
- components
- knock-codes
- core
- react
"use client";
import type { ReactNode } from "react";
import { useKnockCodesContext } from "./KnockCodesProvider.tsx";
import { cx } from "./cx.ts";
export interface LogoutButtonProps {
children?: ReactNode;
className?: string;
/** Called after the shared session is cleared. */
onLoggedOut?: () => void;
}
/**
* Reads the shared session from `<KnockCodesProvider>` and clears it on
* click. Requires a provider — a logout action only makes sense where a
* session is already being shared with something else that reads it.
*/
export function LogoutButton({ children = "Log out", className, onLoggedOut }: LogoutButtonProps) {
const { logout } = useKnockCodesContext();
return (
<button
type="button"
onClick={() => {
logout();
onLoggedOut?.();
}}
className={cx(
"rounded-[var(--ag-radius,0.5rem)] border border-[var(--ag-border,#e5e7eb)] px-3 py-1.5 text-sm font-medium text-gray-700 hover:border-[var(--ag-primary,#3b82f6)]/40 hover:bg-[var(--ag-primary,#3b82f6)]/5 dark:border-[var(--ag-border-dark,#1f2937)] dark:text-gray-200 dark:hover:border-[var(--ag-primary,#3b82f6)]/40 dark:hover:bg-[var(--ag-primary,#3b82f6)]/10",
className
)}
>
{children}
</button>
);
}
Add this block to your project
Recommended
npx shadcn@latest add @knock-codes/logout-buttonAlso installs
- Knock Codes Core
- Knock Codes Types
- useKnockCodes
- Session Provider
- cx (classname helper)
These install together as one atomic unit — even a presentational or read-only piece needs the full verification stack (hook, types, core) behind it to actually run.
Files created (10)
- components/knock-codes/core/hash.ts
- components/knock-codes/core/verify.ts
- components/knock-codes/core/session.ts
- components/knock-codes/core/storage.ts
- components/knock-codes/react/types.ts
- components/knock-codes/react/useKnockCodes.ts
- components/knock-codes/react/KnockCodesContext.tsx
- components/knock-codes/react/KnockCodesProvider.tsx
- components/knock-codes/react/cx.ts
- components/knock-codes/react/LogoutButton.tsx
Other ways
GitHub shorthand
npx shadcn@latest add trivedi-vatsal/knock-codes/logout-buttonCopy the files by hand
- Open the Code tab in the preview above.
- Create each path listed below in your project and paste its contents in.
- Do the same for anything listed under “Also installs”, if present.
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | "Log out" | Button label. |
| className | string | — | Extra classes on the button. |
| onLoggedOut | () => void | — | Called after the shared session is cleared. |
Accessibility
A real <button>, not a link or div — full keyboard operability and screen-reader semantics for free.
Customization
Pass children to change the label, or `onLoggedOut` to run something after the session clears (redirect, analytics, whatever your app needs).
Need a hash? Use the hash generator on Getting Started — computed locally, never sent anywhere.
The honest version
Knock Codes stops casual visitors, search engines, and forwarded links. Local mode does not stop anyone who opens DevTools — the hash ships in your client bundle by design. Server mode (swap one prop) hides the hash from the client; children you already bundled are still in the JavaScript, and a forged session works unless you wire validateSession. A velvet rope, with an optional real lock. Never marketed as more than that.
Blocks that pair well with this one
These combine naturally with this block, whether as a shared shell, a shared session, or a common fallback.
- Session ProviderA copy-paste React session provider for Knock Codes access screens — shares one unlock state across a gate, a logout button, and a timeout banner. Zero dependencies.
- Session Timeout BannerA copy-paste React warning banner for Knock Codes access screens — alerts before a shared session expires, with a one-click logout. Zero dependencies.
- Access ReceiptA copy-paste React session receipt for Knock Codes access screens — unlock time, storage mode, timeout. Zero dependencies.