Session Provider
A 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.
Best used for Any screen where more than one component (a gate, a logout button, a timeout banner) needs the same session.
useKnockCodesContext() throws outside a provider — there's no meaningful standalone default for "give me
the shared session" when nothing established one.
Minimal setup
<KnockCodesProvider expectedHash={process.env.NEXT_PUBLIC_KNOCK_HASH}>
<KnockCodes>
<Dashboard />
</KnockCodes>
</KnockCodesProvider>
- components
- knock-codes
- core
- react
"use client";
import type { ReactNode } from "react";
import { useKnockCodes } from "./useKnockCodes.ts";
import { KnockCodesContext } from "./KnockCodesContext.tsx";
import type { KnockCodesConfig } from "./types.ts";
export { useKnockCodesContext, useOptionalKnockCodesContext } from "./KnockCodesContext.tsx";
export interface KnockCodesProviderProps extends KnockCodesConfig {
children: ReactNode;
}
/**
* Shares one `useKnockCodes` session across a whole tree, so multiple
* components — a gate, a `LogoutButton`, a `SessionTimeoutBanner` — read and
* act on the same unlock state instead of each running an independent
* verification/session lifecycle. Optional: `useKnockCodes` or `<KnockCodes>`
* work standalone with no provider; reach for this only when two or more
* components need to share one session.
*
* Gates under this provider (`<KnockCodes>`, `<ProtectedCard>`, …) join the
* shared session automatically. Put `expectedHash` / `verify` on the
* provider; repeating them on the gate is ignored.
*/
export function KnockCodesProvider({ children, ...config }: KnockCodesProviderProps) {
const state = useKnockCodes(config);
return <KnockCodesContext.Provider value={state}>{children}</KnockCodesContext.Provider>;
}
Add this block to your project
Recommended
npx shadcn@latest add @knock-codes/session-providerAlso installs
- Knock Codes Core
- Knock Codes Types
- useKnockCodes
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 (8)
- 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
Other ways
GitHub shorthand
npx shadcn@latest add trivedi-vatsal/knock-codes/session-providerCopy 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 |
|---|---|---|---|
| expectedHash | string | — | SHA-256 hex hash to verify against, for local mode. |
| verify | VerifyFn | — | Custom async verification function, for server mode. |
| storage | "localStorage" | "sessionStorage" | "memory" | "localStorage" | Where the unlocked session persists. |
| timeout | number | 1800000 | Session lifetime in milliseconds. |
| validateSession | (session: KnockCodesSession) => boolean | Promise<boolean> | — | Called when a session is read from storage. Return false or throw to reject it (server-mode token check). |
| children * | ReactNode | — | Everything that should read the shared session via useKnockCodesContext(). |
Exactly one of expectedHash or verify is required.
Accessibility
Renders no UI of its own — purely a context boundary. Accessibility is entirely a function of whatever you render inside it.
Customization
Reach for this only when two or more components need to share one session — a gate, a Logout Button, and a Session Timeout Banner all reading and acting on the same unlock state. Gates under this provider (`<KnockCodes>`, `<ProtectedCard>`, `<ProtectedModal>`, `<ProtectedRoute>`) join that session automatically instead of spinning up a second hook. Put `expectedHash` / `verify` on the provider; repeating them on the gate is ignored. `useKnockCodes` remains the right choice for a fully custom standalone prompt.
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.
- Logout ButtonA copy-paste React logout control for Knock Codes access screens — clears the shared session on click. 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.
- Knock CodesA copy-paste React access screen for gating a whole page or app root — local hash or server verification, one prop swap between them. Zero dependencies.