Skip to content

Your own user records

Keyhaw gives each user an ID and keeps no other detail about them, so your own records hang off that ID. Create a record in your database the first time an ID appears, and read the ID on each request after that. This page shows how to do it on each framework and what to check before you trust the ID.

Keep the record small. Store the Keyhaw user ID as the key, and the details your app needs, such as an email address you have verified, in your own table. Keyhaw never sees those details.

Create the record the first time an ID appears

Section titled “Create the record the first time an ID appears”

Use an upsert, so a request that finds a row leaves it alone and a request that finds none creates it. Run it on every request that identifies the user, since a user who returns on a new device may be the first request your server sees from them.

// Your database: create the row once, and leave an existing row as it is.
await db.userRecords.upsert({ keyhawId: user.id })

An event hook can also create the record for a sign-up that passes through your server, but it runs at most once and only for the requests that reach your server. The upsert on a request is the one that never misses a user who comes back.

On Next.js, 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.

import { session } from "@keyhaw/next"
const user = await session()
if (user) await db.userRecords.upsert({ keyhawId: user.id })

On Astro, Astro.locals.keyhaw.user() answers the signed-in user, verified without a request to Keyhaw, or null.

---
const user = await Astro.locals.keyhaw.user()
if (user) await db.userRecords.upsert({ keyhawId: user.id })
---

On a static site, the page has no server of its own, so it sends the access token to your API, and your API reads the user from the token. The page gets the token from getAccessToken().

import { getAccessToken } from "keyhaw/client"
const token = await getAccessToken()
await fetch("/api/records", { headers: { Authorization: `Bearer ${token}` } })

Your API checks the token with verifyToken, which returns the user, or null when the token is not valid.

import { verifyToken } from "keyhaw/server"
const user = await verifyToken(token)
if (!user) return new Response("Unauthorized", { status: 401 })
await db.userRecords.upsert({ keyhawId: user.id })

Your API has to check the token, or read the session on the server. Never trust a user ID the page sends in the body of a request, since any visitor can send any ID.

A page that needs the ID before it sends anything can subscribe to the session in the browser. keyhaw.session.subscribe calls its listener with the current session, and again on each sign-in and sign-out.

const stop = keyhaw.session.subscribe((session) => {
render(session)
})

After a sign-up, signUp resolves the new user, with its ID at data.user.id, so the page knows the ID without waiting for the subscription. The page still sends the token from getAccessToken() to your API, and your API still reads the ID from the token, not from anything the page sends with it.

Keyhaw can tell your code about a sign-up, a deletion, and other changes in four ways. Each one runs in a different place and misses a different set of events, so pick the one that matches what you need.

# Way Runs in Sees Delivery
1 session.subscribe The browser, on any site Sign-in and sign-out in that tab Only while the page is open
2 Upsert when a session arrives Your server The first request from each user Never misses a user who comes back
3 Event hook Your server, under an adapter Ceremonies through that server At most once
4 Webhooks, planned for M4 Your endpoint Everything, including a deletion from the dashboard At least once, with retries

The upsert covers a user who comes back, so it is the source of truth for your records. A browser subscription helps a page react at once, but it only runs while the page is open. Until webhooks ship, a deletion made in the dashboard never reaches your server through these paths, so a record can outlive a user the dashboard deleted, with no event on your server to tell you.