Skip to content

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.

Terminal window
npm add keyhaw @keyhaw/react @keyhaw/vite-plugin

The Keyhaw packages appear under dependencies in your package.json.

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.

.env
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.

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.

vite.config.ts
import { keyhaw } from "@keyhaw/vite-plugin"
import react from "@vitejs/plugin-react"
import { defineConfig } from "vite"
export default defineConfig({
plugins: [react(), keyhaw()],
})

Start the dev server with npm run dev, and it starts as it did before.

Render <SignIn /> and import the stylesheet.

src/app.tsx
import { SignIn } from "@keyhaw/react"
import "keyhaw/styles.css"
export function App() {
return <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.

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.

src/account.ts
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.

api/account.ts
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.

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.