Quickstart
This quickstart takes an app from a new Keyhaw instance to a signed-in user in five steps, and ends on a server route that only a signed-in user can open. Pick your framework in any step and the rest of the page follows it.
You need an app on Vite with React, Next.js, or Astro, and an instance in the Keyhaw dashboard. A new instance serves your app on http://localhost at any port, so the app runs locally with nothing else to set up.
Install the packages
Section titled âInstall the packagesânpm add keyhaw @keyhaw/react @keyhaw/vite-pluginnpm add keyhaw @keyhaw/react @keyhaw/next<SignIn /> renders through Astroâs React integration, which astro add react sets up if your app does not have it yet.
npx astro add reactnpm add keyhaw @keyhaw/react @keyhaw/astroThe Keyhaw packages appear under dependencies in your package.json.
Add your keys
Section titled âAdd your keysâCopy the instanceâs keys from its page in the dashboard into .env at the root of your app. The publishable key takes your frameworkâs prefix for a value the browser may read, since it names your instance and is safe to ship.
VITE_KEYHAW_PUBLISHABLE_KEY=pk_...A Vite single-page app has no server of its own, so it needs only the publishable key. If your Vite build includes a server, Keyhaw proxies through it and reads KEYHAW_SECRET_KEY there as well.
NEXT_PUBLIC_KEYHAW_PUBLISHABLE_KEY=pk_...KEYHAW_SECRET_KEY=sk_...PUBLIC_KEYHAW_PUBLISHABLE_KEY=pk_...KEYHAW_SECRET_KEY=sk_...The secret key never takes a prefix, so it stays on your server. The dashboard shows it once, when you create the instance or rotate the key, so copy it then. Keep .env out of version control, and git check-ignore .env prints .env when it is.
Add Keyhaw to your config
Section titled âAdd Keyhaw to your configâimport { keyhaw } from "@keyhaw/vite-plugin"import react from "@vitejs/plugin-react"import { defineConfig } from "vite"
export default defineConfig({ plugins: [react(), keyhaw()],})export { proxy } from "@keyhaw/next"
export const config = { matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],} as constThe proxy serves Keyhaw on your appâs own origin and keeps the session current before each route renders. The matcher leaves out the files Next.js builds, which never read the session, so the proxy does not run for them.
import react from "@astrojs/react"import keyhaw from "@keyhaw/astro"import { defineConfig } from "astro/config"
export default defineConfig({ // Keyhaw reads the session on each request, so pages render on demand output: "server", integrations: [react(), keyhaw()],})Start the dev server with npm run dev, and it starts as it did before.
Render the sign-in form
Section titled âRender the sign-in formâRender <SignIn /> and import the stylesheet.
import { SignIn } from "@keyhaw/react"import "keyhaw/styles.css"
export function App() { return <SignIn />}import { SignIn } from "@keyhaw/next"import "keyhaw/styles.css"
export default function Page() { return <SignIn />}---import { SignIn } from "@keyhaw/astro/react"import "keyhaw/styles.css"---
<SignIn />Open your app and click the button. Your browser first asks for a passkey you have not made yet. If you close that prompt, the button reads âCreate an accountâ, and clicking it again opens the prompt to make one. Approve it, and the button reads âSign outâ once you are signed in. Sign out and click the button again, and your browser signs you in with the passkey you saved.
Gate a route on the server
Section titled âGate a route on the serverâRead the visitorâs session on the server, and turn away a visitor who is signed out.
A Vite single-page app keeps its session with your Keyhaw instance, so the page sends your server an access token with each request that needs the user.
import { getAccessToken } from "keyhaw/client"
export async function getAccount() { const response = await fetch("/api/account", { headers: { Authorization: `Bearer ${await getAccessToken()}` }, }) return response.json()}session() reads the token from the request and returns the signed-in user, or null for a visitor who is signed out. Any server that hands a route a standard Request runs this route, such as Cloudflare Workers, Bun, Deno, or Hono.
import { session } from "keyhaw/server"
export async function GET(request: Request) { const publishableKey = process.env.VITE_KEYHAW_PUBLISHABLE_KEY const user = await session(request, { publishableKey }) if (!user) return new Response(null, { status: 401 }) return Response.json({ id: user.id })}Signed in, getAccount() returns your user ID. Signed out, the route answers 401.
session() from @keyhaw/next reads the session from the request and returns the signed-in user, or null for a visitor who is signed out. It works the same in a route handler or a server action.
import { session } from "@keyhaw/next"import { redirect } from "next/navigation"
export default async function Account() { const user = await session() if (!user) redirect("/")
return ( <p> Signed in as user <code>{user.id}</code> </p> )}Open /account while you are signed in, and it shows your user ID. Sign out and open it again, and it sends you back to the sign-in page.
The Astro integration reads the session for every page, and Astro.locals.keyhaw.user() returns the signed-in user, or null for a visitor who is signed out.
---const user = await Astro.locals.keyhaw.user()if (!user) return Astro.redirect("/")---
<p>Signed in as user <code>{user.id}</code></p>Open /account while you are signed in, and it shows your user ID. Sign out and open it again, and it sends you back to the sign-in page.
Your app now signs in and gates a route locally. Going to production takes it to a production instance on your domain, with its own allowed origins and keys.