@user-hub/auth 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,219 +1,41 @@
1
1
  ---
2
2
  name: user-hub-auth
3
- description: Sign people into an app from their User Hub badge. Use when an app needs User Hub auth (workshop badges, QR sign-in, hubAuth, ConvexHubAuthProvider, HubAuthProvider, HubSignedIn, useHubUser, verifyHubToken, hub-user login, hub-user events, hub-user register), or when asked to connect an app to the hub. Never hand-roll login, passwords or token checks for these apps.
3
+ description: Sign people into an app from their printed User Hub badge with @user-hub/auth. Use when connecting an app to a User Hub, adding badge or QR sign-in, wiring hubAuth, ConvexHubAuthProvider, HubAuthProvider, HubSignedIn, useHubUser or verifyHubToken, building facilitator-only pages, or moving an app to a new hub URL or to production. Use it even when the user only says "connect this to the hub" or "add sign-in for the workshop". Never hand-roll login, passwords or token checks for these apps.
4
4
  ---
5
5
 
6
6
  # User Hub auth
7
7
 
8
- People at a workshop scan a printed QR badge. One hub runs many workshops, called **events**; each person and each app belongs to one. The hub sends them, signed in, to whichever app is live in their event (or shows that event's app list so they pick one), with a token in the URL fragment (`#token=…`) that only that app accepts. The app gets the same token either way and needs nothing extra for the app list. This package turns that token into a signed-in person. The app keeps its own data, keyed on the person's hub id.
8
+ People at a workshop scan a printed QR badge. One hub runs many workshops, called **events**; every person and every app belongs to one. The scan sends the person, signed in, to the app that is live in their event (or to the event's app list), with a token in the URL fragment (`#token=…`) that only that app accepts. `@user-hub/auth` turns that token into a signed-in person: `{ id, name, team, role }`. The app keeps its own data, keyed on the person's hub id.
9
9
 
10
- ## Starting a new app
10
+ Registering apps, signing a folder in to the hub and managing people is done with the `user-hub` CLI: follow the **user-hub-cli** skill for any `user-hub …` command.
11
11
 
12
- If there is no app yet, don't build one by hand: scaffold one with sign-in already wired (TanStack Start + Convex, sign-in and sign-out, a facilitator page, `CLAUDE.md` and this skill):
12
+ ## Pick the path
13
13
 
14
- ```sh
15
- bun create user-hub@latest my-app
16
- ```
14
+ | Situation | Do this |
15
+ |---|---|
16
+ | No app yet | Scaffold one: `bun create user-hub@latest my-app`, then `bun run setup` and `bun run dev` in it. It comes wired; read on only to change the wiring |
17
+ | Existing app, not connected | Follow [references/connect-an-app.md](references/connect-an-app.md): install, register, set the values, wire the provider, use the person |
18
+ | Facilitator-only pages or actions | [references/roles.md](references/roles.md) |
19
+ | Running it locally, going to production, or the hub moved | [references/dev-and-production.md](references/dev-and-production.md) |
17
20
 
18
- In a terminal it asks for the hub's URL, then signs the new app in (`hub-user login`: paste an API key, or approve a sign-in in the browser) and saves `HUB_URL` and `HUB_API_KEY` to the new app's `.env.local`. As an agent you never handle an API key, so either ask the human to run it, or run it without a terminal with the hub's URL (`bun create user-hub@latest my-app --hub-url https://hub.example.com`) and then sign the new app in from its folder as described in **Signing in** below.
21
+ A starter app is one whose `package.json` has `"setup": "user-hub setup"`: for it, prefer `bun run setup` (with `--event`, `--prod` or `--dev`) over doing the steps by hand.
19
22
 
20
- Then:
23
+ ## Ask the human first
21
24
 
22
- ```sh
23
- cd my-app
24
- bun run setup # Convex project, hub registration, HUB_ISSUER and HUB_JWKS, once
25
- bun run dev # http://localhost:3000
26
- ```
25
+ Never guess these; ask:
27
26
 
28
- In a terminal, `bun run setup` asks which event the app is for (or creates one) and saves it as `HUB_EVENT` in `.env.local`. Without a terminal (as an agent), ask the human which event (**Before you start**) and pass it: `bun run setup --event <slug>`.
27
+ 1. **Which event** the app is for (the CLI lists them). Create an event only when the human asks.
28
+ 2. **The app's name** in the hub, unique within the event.
29
+ 3. **Its local URL** (usually `http://localhost:3000`) and its **production URL** once deployed.
30
+ 4. **Which Convex deployments** it uses (dev, prod), so the values go on each.
29
31
 
30
- That covers every step below; read on only to add User Hub to an existing app or to change the wiring.
31
-
32
- ## The hub's URL
33
-
34
- The hub has one address, for example `https://hub.example.com`. The human gives it to you; you need it in three places, all with the same value:
35
-
36
- - `HUB_URL` in the app's `.env.local`: read by `hub-user register` (**Register the app**). `hub-user login` puts it there, next to the API key (**Signing in**).
37
- - `HUB_ISSUER` on the app's Convex deployment: `hub-user register` prints it for you (**Set the two values**).
38
- - `VITE_HUB_URL` in the app's frontend env, for production builds too: lets the dev server send you to the hub to sign in, and limits token refresh to that hub (**Wire it up**).
39
-
40
- ## Before you start: ask the human
41
-
42
- You need five things. Ask for them; never guess.
43
-
44
- 1. **Which event** the app is for. Run `bunx -p @user-hub/auth hub-user events` once the folder is signed in (question 4) to list the events and their slugs, and let the human pick one, or create one when they ask for it (`bunx -p @user-hub/auth hub-user events create --name "<event name>"`; ask the human for the event's time zone and pass it as `--time-zone <IANA zone>`, such as `Europe/Berlin`: it defaults to this machine's, which in a sandbox is often UTC). If `HUB_EVENT` is already in the app's environment (a starter app's `bun run setup` saves it to `.env.local`), that is the app's event: check it with the human only when asked to move the app.
45
- 2. **The app's name** in the hub, unique to this app within the event (the same event and name update the same app, and apps copied from a template often share a package name).
46
- 3. **Its local URL** (usually `http://localhost:3000`) and **its production URL** if it is deployed. Without a production URL the app can be developed but not scheduled or shown in the app list, so badge scans never reach it; add it later by running register again with `--prod`.
47
- 4. **Whether the app's folder is signed in to the hub.** Check it yourself with `bunx -p @user-hub/auth hub-user whoami` (it prints the account, or exits with code 4 when not signed in). If it is not, see **Signing in**. If the human keeps `HUB_URL` and `HUB_API_KEY` in a secret manager (Infisical, say), run every `hub-user` command through it instead (`infisical run -- bunx -p @user-hub/auth hub-user register …`).
48
- 5. **Which Convex deployments** the app uses (dev, prod), so the values go on each.
49
-
50
- ## Signing in
51
-
52
- `hub-user` acts as a hub account through an API key, `HUB_API_KEY`, saved in the app folder's `.env.local` next to `HUB_URL`. You never ask for an API key or password in chat, never write one into a file yourself, never commit one and never print `.env.local`. Always run the CLI as `bunx -p @user-hub/auth hub-user …`: it uses the app's installed `@user-hub/auth`, or fetches that package when the app does not have it yet. Never run a bare `bunx hub-user`: in a folder without the package it fetches an npm package called `hub-user`, which is not ours.
53
-
54
- Sign the folder in one of two ways, from the app's folder:
55
-
56
- - **The human runs** `bunx -p @user-hub/auth hub-user login --hub https://<hub url>` in a terminal. It asks them to paste an API key from `<hub>/account` (it shows as stars) or press Enter to sign in with the browser.
57
- - **You run** `bunx -p @user-hub/auth hub-user login --hub https://<hub url> --browser`. It prints a link and a code, then waits (up to 10 minutes) until the request is approved: run it in the background or with a long timeout, read the link from its output and give it to the human to open, sign in to the hub and approve. Never open or approve the link yourself. When it is approved, the command creates an API key for itself, saves it and prints `Signed in to <hub> as <username> (<role>)`.
58
-
59
- Hubs with accounts need `@user-hub/auth` 0.3 or newer. If the app already has an older one installed (its `hub-user` has no `login`), run `bun add @user-hub/auth@latest` first.
60
-
61
- Hubs with events need `@user-hub/auth` 0.4 or newer. If the app is on 0.3 (a `^0.3` range never installs 0.4; its `hub-user` has no `events`), run `bun add @user-hub/auth@latest` first.
62
-
63
- The hub's URL comes from the human (or `HUB_URL` already in `.env.local`, which `--hub` can then be left out for). If login fails because the request was denied or expired, run it again or ask the human. An older `.env.local` with `HUB_ADMIN_PASSWORD` is no longer used: ask the human to delete that line.
64
-
65
- ## Steps
66
-
67
- 1. **Install:**
68
-
69
- ```sh
70
- bun add @user-hub/auth
71
- ```
72
-
73
- 2. **Register the app** in its event from the app's repo root, once the folder is signed in (question 4 above):
74
-
75
- ```sh
76
- bunx -p @user-hub/auth hub-user register --event <slug> --name "<app name>" --new --prod https://app.example.com --dev http://localhost:3000
77
- ```
78
-
79
- `--event` is needed on every run; with `HUB_EVENT` in the environment (`.env.local` is read too) it can be left out (`--event` wins over it). Do not edit `.env.local` yourself: it holds the API key. With neither it fails and says how to choose one. If it exits with code 5, no event has that slug: run `hub-user events`, check the slug with the human and run it again; never create an event unless the human asks.
80
-
81
- Always quote a name or other value that contains spaces (`--name "Summer Camp"`), here and in `hub-user events create`. If a `hub-user` command exits with code 1 and "unexpected argument", it made no request and changed nothing: fix the quoting and run it again. When you pass `--hub`, the commands the CLI suggests next keep it, so they can be run as printed.
82
-
83
- `--new` makes sure you never take over another app: if the name is already taken, nothing changes and it exits with code 3 and "already exists". The same code 3 comes without `--new` when an app of another event holds the key. Then ask the human for another name and run it again. Drop `--new` only when updating this app's own registration later.
84
-
85
- Leave out `--prod` if the app is not deployed yet. Running it again with the same event and `--name` updates the URLs; a URL left out keeps the registered one, and with no URL flags it keeps the registered URLs and just prints the values. Use the exact name the app has in the hub: the hub finds the app by the key made from the event's slug and the name and saves the name as you typed it. If it exits with code 4, the hub did not accept this folder's sign-in ("not signed in" or "did not accept HUB_API_KEY"): sign in again (**Signing in**); do not work around it. Any hub account can update any app, so register only under this app's own name, never another app's.
86
-
87
- New apps are **listed** in the badge app list by default. When whoever runs the hub sets **Badges go to** to the app list, a scan shows the listed apps that have a production URL and the person picks one; a listed app without a production URL does not appear. Pass `--unlisted` to keep one out of it (it can still be scheduled or forced live), or `--listed` to show it again; in a starter app whose `scripts/setup.ts` mentions `--unlisted`, pass it to `bun run setup`. Pass neither when updating and the hub's current setting stays; the output shows it ("Listed in the badge app list: yes", or "yes, once it has a production URL" without `--prod`). If it says the hub "did not apply" the flag, the hub is too old for the app list: tell the human; the rest of the registration went through.
88
-
89
- It prints the app **key** (`<event slug>-<name>`), `HUB_ISSUER` and `HUB_JWKS` (server side) and `VITE_HUB_URL` (frontend). Add `--json` to parse them (it includes `event: { slug, name }`).
90
-
91
- 3. **Set the two values** where tokens are checked:
92
- - Convex: `bunx convex env set HUB_ISSUER '…'` and `bunx convex env set HUB_JWKS '…'` (on every deployment the app uses).
93
- - Other servers: their environment, same names.
94
-
95
- 4. **Wire it up** with the key from **Register the app**.
96
-
97
- Convex, `convex/auth.config.ts`:
98
-
99
- ```ts
100
- import type { AuthConfig } from "convex/server";
101
- import { hubAuth } from "@user-hub/auth/convex";
102
-
103
- export default { providers: [hubAuth({ app: "<key>" })] } satisfies AuthConfig;
104
- ```
105
-
106
- The provider replaces `ConvexProvider` (keep the same `ConvexReactClient`). Set `VITE_HUB_URL` (the hub's URL) in the app's frontend env.
107
-
108
- **Where it goes:** inside the root document's `<body>`, around the page content. In TanStack Start that is the root route's shell component (`src/routes/__root.tsx`); in a Vite SPA, around `<App />`; in Next.js, inside `<body>` in the root layout. **Never** in the router's `Wrap`, or anywhere outside `<html>`.
109
-
110
- ```tsx
111
- import { ConvexHubAuthProvider, HubSignedIn } from "@user-hub/auth/convex-react";
112
-
113
- <body>
114
- <ConvexHubAuthProvider
115
- client={convex}
116
- app="<key>"
117
- hubUrl={import.meta.env.VITE_HUB_URL}
118
- >
119
- <HubSignedIn>{children}</HubSignedIn>
120
- </ConvexHubAuthProvider>
121
- <Scripts />
122
- </body>
123
- ```
124
-
125
- The provider always renders its children; `<HubSignedIn>` shows them only to a signed-in person. Without a valid token it shows "Scan your badge to sign in": pass `signedOut={…}` to style that screen and `loading={…}` for the first render. Anything outside `<HubSignedIn>` (a public landing page, say) shows to everyone. Apps without Convex use `HubAuthProvider` and `HubSignedIn` from `@user-hub/auth/react`, with the same props minus `client`.
126
-
127
- 5. **Use the person.**
128
- - In components: `const me = useHubUser()` gives `{ id, name, team, role }`.
129
- - In Convex functions: `const me = await requireHubUser(ctx)` gives the same `{ id, name, team, role }` (see Facilitator pages).
130
- - Not Convex: wrap the app in `HubAuthProvider` and `HubSignedIn` (`@user-hub/auth/react`), send `useHubToken()` as `Authorization: Bearer <token>`, and check it on the server with `await verifyHubToken(request, { app: "<key>" })` from `@user-hub/auth/server`.
131
-
132
- ## Your dev server
133
-
134
- Run the app as usual (`bun run dev`) on the `--dev` URL you registered. Opening it with no token sends the browser to the hub (the human signs in with their hub account once per browser), where you pick who to be; the hub sends you back signed in as them. You stay signed in across reloads and restarts until that person's expiration in the hub (4 hours unless set otherwise), then the same thing happens again.
135
-
136
- - To switch person: open `<hub>/open/<key>` and pick someone else.
137
- - If the dev server runs on another port, run `hub-user register` again with the same event and `--name` and the new `--dev` URL: the hub only sends tokens back to registered URLs.
138
- - This only happens on local addresses (localhost, 127.x, 10.x, 172.16-31.x, 192.168.x, *.local). In production a person without a token sees "Scan your badge to sign in".
139
-
140
- ## Production URL
141
-
142
- Badge scans only reach an app once the hub knows its production URL. When the human asks to set up the production URL (or says the app is deployed), ask for the deployed URL (never guess), then:
143
-
144
- - **Starter apps** (they have `scripts/setup.ts`): `bun run setup --prod https://<deployed url>`. It updates the app's own registration and sets `HUB_ISSUER` and `HUB_JWKS` on the production Convex deployment too.
145
- - **Other apps:** `bunx -p @user-hub/auth hub-user register --event <slug> --name "<the app's name in the hub>" --prod https://<deployed url>`, without `--new` (this updates the app's own registration; the event is `HUB_EVENT` and the name `HUB_APP_NAME` in `.env.local`, or ask the human). Then set the two printed values on production: `bunx convex env set --prod HUB_ISSUER '…'` and `bunx convex env set --prod HUB_JWKS '…'`.
146
-
147
- Then:
148
-
149
- 1. `bunx convex deploy`.
150
- 2. On the host, set `VITE_CONVEX_URL` (the production deployment's URL) and `VITE_HUB_URL` (the hub's URL).
151
- 3. Tell the human the app is ready to be scheduled (or forced live) under **Schedule** of its event in the hub; until then badge scans go to whichever app is live in that event. While **Badges go to** shows the app list, a listed app appears there as soon as it has its production URL.
152
-
153
- ## Hub moved to a new URL
154
-
155
- When the human says the hub's URL changed, hub ids stay the same, so data keyed on the id carries over. Until the app is updated it rejects every sign-in.
156
-
157
- - **Starter apps** (they have `scripts/setup.ts`): sign the folder in to the new hub with `hub-user login --hub <new url>` (**Signing in**), then run `bun run setup`. It updates the app's own registration and sets `HUB_ISSUER`, `HUB_JWKS` and `VITE_HUB_URL` again (on the production Convex deployment too, once the app has a production URL). Then redeploy with `just push`: Vite bakes `VITE_HUB_URL` in at build time.
158
- - **Other apps:**
159
- 1. Sign the app's folder in to the new hub with `hub-user login --hub <new url>` (**Signing in**); it replaces `HUB_URL` and `HUB_API_KEY` in `.env.local`. If the human keeps them in a secret manager, ask them to change them there.
160
- 2. Run `bunx -p @user-hub/auth hub-user register --event <slug> --name "<the app's name in the hub>"` without `--new` or any URL flags (`--event` can be left out when `HUB_EVENT` is in `.env.local`): it keeps the registered URLs and prints the new values. Use the event's slug and the exact name from the event's **Apps** page in the hub: the CLI finds the app by the key made from both. The printed key must equal the `app` key already in the code; if it differs, or the hub says to give a URL (no app has that name), stop and ask the human: you found another app, or none.
161
- 3. Set `HUB_ISSUER` and `HUB_JWKS` on every Convex deployment the app uses (`bunx convex env set …`, and again with `--prod`). Convex re-reads `auth.config.ts` with the new values straight away; no deploy is needed.
162
- 4. Set `VITE_HUB_URL` to the new URL in the frontend env and on the host, then rebuild and redeploy the frontend: Vite bakes it in at build time.
163
-
164
- ## Sign out
165
-
166
- Use `useHubSignOut()` from the same entry point as the provider; never clear storage or tokens yourself.
167
-
168
- ```tsx
169
- const signOut = useHubSignOut();
170
- <button onClick={signOut}>Sign out</button>
171
- ```
172
-
173
- It forgets this app's token only. In production the person then sees the signed-out screen and scans their badge again; on a local dev server the hub opens to pick another person.
174
-
175
- ## Facilitator pages
176
-
177
- People are either `"member"` or `"facilitator"`, set in the hub. To give facilitators their own page or actions:
178
-
179
- - **Protect the data on the server, always.** In Convex functions use the helpers from `@user-hub/auth/convex`:
180
-
181
- ```ts
182
- import { requireFacilitator, requireHubUser } from "@user-hub/auth/convex";
183
-
184
- export const closeVoting = mutation({
185
- args: {},
186
- handler: async (ctx) => {
187
- const facilitator = await requireFacilitator(ctx); // throws for members
188
- // …
189
- },
190
- });
191
-
192
- export const myVotes = query({
193
- args: {},
194
- handler: async (ctx) => {
195
- const me = await requireHubUser(ctx); // throws when signed out
196
- // query by me.id
197
- },
198
- });
199
- ```
200
-
201
- `getHubUser(ctx)` returns the person or null when signing in is optional. Not Convex: check `(await verifyHubToken(request, { app })).role`.
202
-
203
- - **Then hide what members cannot use**, for a clean UI only:
204
-
205
- ```tsx
206
- const me = useHubUser();
207
- if (me?.role !== "facilitator") return <p>This page is for facilitators.</p>;
208
- ```
209
-
210
- A hidden page is not protection: members can still call the functions, which is why the server check comes first.
32
+ The hub's URL also comes from the human. It goes in three places with the same value: `HUB_URL` (saved by `user-hub login`), `HUB_ISSUER` on Convex, and `VITE_HUB_URL` in the frontend env.
211
33
 
212
34
  ## Rules
213
35
 
214
- - Key all data on the hub id (`identity.subject` / `useHubUser().id`). Never on the name, never on the badge token, which the app never sees, and never on Convex's `identity.tokenIdentifier`, which includes the hub's URL and changes if the hub moves.
215
- - Check access on the server: `requireFacilitator(ctx)` / `requireHubUser(ctx)` in Convex, or `verifyHubToken` elsewhere. `useHubUser()` is for display only.
216
- - The provider refreshes tokens with the hub (`<hub>/api/token/refresh`) while the app is open; the hub decides how long a session lasts and when it ends, and the provider then shows the signed-out screen. Do not refresh, store or extend tokens yourself. Read `useHubToken()` for each request, since it changes. If the app sets a Content Security Policy, allow the hub in `connect-src`.
217
- - Do not add other sign-in methods, sessions or token handling. If something is missing, change this package instead.
218
- - `@user-hub/auth` 0.3 replaced the hub's admin password with accounts: `hub-user` no longer reads `HUB_ADMIN_PASSWORD` and needs `hub-user login` once per app folder. A `^0.2` range never installs 0.3: `bun add @user-hub/auth@latest`; no app code changes.
219
- - An existing app on `@user-hub/auth` 0.1 does not refresh tokens, and a `^0.1` range never installs 0.2: update it with `bun add @user-hub/auth@latest`. No code changes are needed.
36
+ - **Key data on the hub id** (`useHubUser().id`, `identity.subject` in Convex). Never on the name, the badge, or Convex's `tokenIdentifier`, which contains the hub's URL and changes if the hub moves.
37
+ - **Check access on the server**: `requireHubUser(ctx)` / `requireFacilitator(ctx)` in Convex, `verifyHubToken` elsewhere. `useHubUser()` is for display; hiding a page protects nothing.
38
+ - **Mount the provider inside `<body>`**, around the page content, never outside `<html>` or in the router's `Wrap`. It always renders its children; `<HubSignedIn>` does the gating.
39
+ - **Let the provider own the token.** It refreshes it with the hub while the app is open and signs the person out when the hub ends the session. Do not store, refresh or extend tokens yourself; read `useHubToken()` for each request.
40
+ - **No other sign-in.** No login screens, passwords or sessions. If something is missing, it belongs in `@user-hub/auth`.
41
+ - **Secrets stay out of chat and files you write.** The API key lives only in `.env.local`, saved by `user-hub login`.
@@ -0,0 +1,67 @@
1
+ # Connect an existing app
2
+
3
+ Five steps, in order. The `user-hub …` commands are explained in the **user-hub-cli** skill.
4
+
5
+ ## 1. Install
6
+
7
+ ```sh
8
+ bun add @user-hub/auth
9
+ bun add -d user-hub
10
+ ```
11
+
12
+ ## 2. Register the app
13
+
14
+ From the app's folder, once it is signed in (`bunx user-hub whoami`; if it exits with code 4, sign in as the user-hub-cli skill describes):
15
+
16
+ ```sh
17
+ bunx user-hub apps register --event <slug> --name "<app name>" --new --dev http://localhost:3000 [--prod https://app.example.com]
18
+ ```
19
+
20
+ - `--new` never takes over another app: a taken name exits with code 3 and changes nothing. Ask the human for another name. Drop `--new` only when updating this app's own registration later.
21
+ - Leave out `--prod` until the app is deployed.
22
+ - It prints the app **key** (`<event slug>-<name>`), `HUB_ISSUER`, `HUB_JWKS` and `VITE_HUB_URL`. With `--json`: `key`, `env`, `hubUrl`.
23
+
24
+ ## 3. Set the values
25
+
26
+ - Convex, on every deployment the app uses: `bunx convex env set HUB_ISSUER '…'` and `bunx convex env set HUB_JWKS '…'` (add `--prod` for production).
27
+ - Other servers: the same two names in their environment.
28
+ - Frontend env: `VITE_HUB_URL` (the hub's URL), for production builds too.
29
+
30
+ ## 4. Wire it up
31
+
32
+ `convex/auth.config.ts`:
33
+
34
+ ```ts
35
+ import type { AuthConfig } from "convex/server";
36
+ import { hubAuth } from "@user-hub/auth/convex";
37
+
38
+ export default { providers: [hubAuth({ app: "<key>" })] } satisfies AuthConfig;
39
+ ```
40
+
41
+ The provider replaces `ConvexProvider` (same `ConvexReactClient`), **inside the root document's `<body>`**: in TanStack Start the root route's shell component (`src/routes/__root.tsx`), in a Vite SPA around `<App />`, in Next.js inside `<body>` of the root layout.
42
+
43
+ ```tsx
44
+ import { ConvexHubAuthProvider, HubSignedIn } from "@user-hub/auth/convex-react";
45
+
46
+ <body>
47
+ <ConvexHubAuthProvider client={convex} app="<key>" hubUrl={import.meta.env.VITE_HUB_URL}>
48
+ <HubSignedIn>{children}</HubSignedIn>
49
+ </ConvexHubAuthProvider>
50
+ <Scripts />
51
+ </body>
52
+ ```
53
+
54
+ `<HubSignedIn>` shows its children only to a signed-in person and "Scan your badge to sign in" otherwise; style that with `signedOut={…}` and the first render with `loading={…}`. Anything outside it (a public landing page) shows to everyone.
55
+
56
+ **Without Convex:** `HubAuthProvider` and `HubSignedIn` from `@user-hub/auth/react`, same place, same props minus `client`.
57
+
58
+ ## 5. Use the person
59
+
60
+ | Where | How |
61
+ |---|---|
62
+ | Components | `const me = useHubUser()` → `{ id, name, team, role }` or `null` |
63
+ | Convex functions | `const me = await requireHubUser(ctx)` (throws when signed out); `getHubUser(ctx)` when signing in is optional |
64
+ | Other servers | Send `useHubToken()` as `Authorization: Bearer <token>`; check it with `await verifyHubToken(request, { app: "<key>" })` from `@user-hub/auth/server` |
65
+ | Sign out | `const signOut = useHubSignOut()` from the provider's entry point. It forgets this app's token only |
66
+
67
+ If the app sets a Content Security Policy, allow the hub in `connect-src`: the provider calls `<hub>/api/token/refresh`.
@@ -0,0 +1,30 @@
1
+ # Local development, production and a moved hub
2
+
3
+ ## Your dev server
4
+
5
+ Run the app on the development URL registered with the hub. With no token, it sends the browser to the hub, where the human (signed in to the hub once per browser) picks who to be, and comes back signed in. That lasts across reloads until the person's session in the hub ends (4 hours unless set otherwise).
6
+
7
+ - **Switch person:** open `<hub>/open/<key>`.
8
+ - **Another port:** the hub only sends tokens to registered URLs. Starter apps: `bun run setup --dev http://localhost:<port>` and change the port in the `dev` script too. Others: `bunx user-hub apps register` again with the same event, `--name` and the new `--dev`.
9
+ - **Test people:** `bunx user-hub people create --name "Test Person" --team Test` (user-hub-cli skill).
10
+ - This redirect only happens on local addresses (localhost, 127.x, 10.x, 172.16-31.x, 192.168.x, *.local). In production a person without a token sees "Scan your badge to sign in".
11
+
12
+ ## Production URL
13
+
14
+ Badge scans reach the app only once the hub knows its production URL. Ask the human for the deployed URL (never guess), then:
15
+
16
+ - **Starter apps:** `bun run setup --prod https://<deployed url>`. It also sets `HUB_ISSUER` and `HUB_JWKS` on the production Convex deployment.
17
+ - **Other apps:** `bunx user-hub apps register --event <slug> --name "<the app's name>" --prod https://<deployed url>` without `--new`, then `bunx convex env set --prod HUB_ISSUER '…'` and `… HUB_JWKS '…'`.
18
+
19
+ Then `bunx convex deploy`, set `VITE_CONVEX_URL` and `VITE_HUB_URL` on the host, and tell the human the app can be scheduled or forced live under **Schedule** of its event. While the event's badges go to the app list, a listed app with a production URL appears there straight away.
20
+
21
+ ## The hub moved to a new URL
22
+
23
+ Hub ids stay the same, so data keyed on the id carries over, but the app rejects every sign-in until it is updated.
24
+
25
+ 1. Sign the folder in to the new hub: `bunx user-hub login --hub <new url>`.
26
+ 2. **Starter apps:** `bun run setup --hub-moved` (it updates the same app on the new hub; add `--event <slug>` if the new hub has no event with the saved slug), then redeploy with `just push`: `VITE_HUB_URL` is built into the frontend.
27
+ 3. **Other apps:**
28
+ - `bunx user-hub apps register --event <slug> --name "<the app's name>"` with no `--new` and no URLs: it keeps the registered URLs and prints the new values. The printed key must equal the `app` key in the code; if it differs, or the hub asks for a URL, stop and ask the human.
29
+ - Set `HUB_ISSUER` and `HUB_JWKS` on every Convex deployment (with and without `--prod`). No deploy needed for that.
30
+ - Set `VITE_HUB_URL` to the new URL, then rebuild and redeploy the frontend.
@@ -0,0 +1,39 @@
1
+ # Facilitator pages
2
+
3
+ People are `"member"` or `"facilitator"`, set in the hub. Protect on the server first, then hide in the UI.
4
+
5
+ ## Server: the real protection
6
+
7
+ ```ts
8
+ import { requireFacilitator, requireHubUser } from "@user-hub/auth/convex";
9
+
10
+ export const closeVoting = mutation({
11
+ args: {},
12
+ handler: async (ctx) => {
13
+ await requireFacilitator(ctx); // throws for members and for nobody signed in
14
+ // …
15
+ },
16
+ });
17
+
18
+ export const myVotes = query({
19
+ args: {},
20
+ handler: async (ctx) => {
21
+ const me = await requireHubUser(ctx); // throws when signed out
22
+ return await ctx.db
23
+ .query("votes")
24
+ .withIndex("by_person", (q) => q.eq("personId", me.id))
25
+ .take(50);
26
+ },
27
+ });
28
+ ```
29
+
30
+ Not Convex: check `(await verifyHubToken(request, { app })).role`.
31
+
32
+ ## UI: only for a clean screen
33
+
34
+ ```tsx
35
+ const me = useHubUser();
36
+ if (me?.role !== "facilitator") return <p>This page is for facilitators.</p>;
37
+ ```
38
+
39
+ Members can still call the functions behind a hidden page, which is why the server check comes first.
package/dist/cli.d.ts DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env bun
2
- export {};