@user-hub/auth 0.2.0 → 0.2.1

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.
package/README.md CHANGED
@@ -8,7 +8,7 @@ Works with Convex apps, React frontends and any server that can check a token.
8
8
 
9
9
  ## How it works
10
10
 
11
- 1. A person scans their badge. The hub sends them to your app with a token in the URL: `https://your-app.com/#token=…`.
11
+ 1. A person scans their badge. The hub sends them to the app that is live, or shows its app list and sends them to the one they pick, with a token in the URL: `https://your-app.com/#token=…`. Your app handles both the same way.
12
12
  2. The provider takes the token out of the address bar and keeps it in the browser, separately for each app.
13
13
  3. Convex (or your server) checks the token with the hub's public key. A token is only accepted by the app it was made for.
14
14
  4. Your app stores its own data under the person's `id`.
@@ -53,7 +53,7 @@ Then, from your app's folder (`bunx` reads `.env` and `.env.local` automatically
53
53
  bunx hub-user register --name "Your App" --new --prod https://your-app.com --dev http://localhost:3000
54
54
  ```
55
55
 
56
- It prints your **app key** (made from the name) and the values to set. Not deployed yet? Leave out `--prod`: you can develop, and badge scans start reaching the app once you add it. Run it again with the same `--name` to update the URLs; a URL left out keeps the registered one. `--new` refuses to touch an app that already has that name, so you never take over someone else's; leave it out when updating your own app later. (A hub admin can do the same under **Apps** in the hub, and copy the values from **Setup**.)
56
+ It prints your **app key** (made from the name) and the values to set. Not deployed yet? Leave out `--prod`: you can develop, and badge scans start reaching the app once you add it. Run it again with the same `--name` to update the URLs; a URL left out keeps the registered one. `--new` refuses to touch an app that already has that name, so you never take over someone else's; leave it out when updating your own app later. New apps are **listed** in the badge app list by default: when the hub shows the app list instead of one live app, listed apps with a production URL appear in it. Pass `--unlisted` to keep one out of it (it can still be scheduled or forced live), or `--listed` to show it again. Pass neither on an update and the hub's current setting stays; the output shows it. `--listed`, `--unlisted` and updates without a URL need @user-hub/auth 0.2.1 or newer (`bun add @user-hub/auth@latest`). (A hub admin can do the same under **Apps** in the hub, and copy the values from **Setup**.)
57
57
 
58
58
  ### 3. Set the values
59
59
 
@@ -181,16 +181,18 @@ Apps without Convex use `HubAuthProvider` and `HubSignedIn` from `@user-hub/auth
181
181
  | `@user-hub/auth/react` | `HubAuthProvider` | The same without Convex: `app`, `hubUrl?` |
182
182
  | both | `HubSignedIn` | Shows its children only when signed in. Props: `signedOut?`, `loading?` |
183
183
  | | `useHubUser()`, `useHubToken()`, `useHubSignOut()` | The person (or null), the current token (or null; it changes as it is refreshed), a sign-out function. Also from `/convex-react` |
184
+ | | `useHubSession()` | `undefined` while loading, `null` when signed out, otherwise `{ token, user }`; most apps need only `useHubUser()` or `<HubSignedIn>`. Also from `/convex-react` |
184
185
  | `@user-hub/auth/server` | `verifyHubToken(requestOrToken, { app })` | Checks a token anywhere else |
185
- | CLI | `hub-user register --name <name> [--prod <url>] [--dev <url>] [--new] [--hub <url>] [--json]` | Registers or updates the app (at least one URL); `--new` only creates, exit code 3 if the name is taken |
186
+ | CLI | `hub-user register --name <name> [--prod <url>] [--dev <url>] [--listed\|--unlisted] [--new] [--hub <url>] [--json]` | Registers or updates the app (a new app needs at least one URL; an update may leave both out); `--new` only creates, exit code 3 if the name is taken; `--listed`/`--unlisted` set whether the app shows in the badge app list once it has a production URL (new apps default to listed, omitted keeps the hub's setting) |
186
187
 
187
188
  ## Developing this package
188
189
 
189
190
  ```sh
190
191
  bun install
191
- bun test # unit tests
192
- bun run check # Biome
193
- bun run build # dist/: commit it
192
+ bun test # unit tests
193
+ bunx tsc --noEmit -p . # type-check
194
+ bun run check # Biome
195
+ bun run build # dist/: commit it
194
196
  ```
195
197
 
196
198
  Release with `just release patch` (or `minor` / `major`): it bumps the version, publishes to npm and pushes the commit and tag. See the comments in the `justfile`.
package/dist/cli.js CHANGED
@@ -2,19 +2,25 @@
2
2
 
3
3
  // src/cli.ts
4
4
  import { parseArgs } from "node:util";
5
- var USAGE = `Usage: hub-user register --name <name> [--prod <url>] [--dev <url>] [--new] [--hub <url>] [--json]
5
+ var USAGE = `Usage: hub-user register --name <name> [--prod <url>] [--dev <url>] [--listed|--unlisted] [--new] [--hub <url>] [--json]
6
6
 
7
- --name The app's name in the hub; its key comes from it (required)
8
- --prod Where badge scans send people. Leave it out until the app is
9
- deployed: it can be developed, but not scheduled.
10
- --dev Where developers open the app as a person, e.g. http://localhost:3000
11
- --new Only create a new app: if the name is taken, change nothing and
12
- exit with code 3 so you can pick another name
13
- --hub The hub's URL (default: HUB_URL)
14
- --json Print JSON instead of text
7
+ --name The app's name in the hub; its key comes from it (required)
8
+ --prod Where badge scans send people. Leave it out until the app is
9
+ deployed: it can be developed, but not scheduled or shown in
10
+ the badge app list.
11
+ --dev Where developers open the app as a person, e.g. http://localhost:3000
12
+ --listed Show the app in the hub's badge app list once it has a
13
+ production URL (default for new apps)
14
+ --unlisted Keep the app out of the hub's badge app list
15
+ --new Only create a new app: if the name is taken, change nothing and
16
+ exit with code 3 so you can pick another name
17
+ --hub The hub's URL (default: HUB_URL)
18
+ --json Print JSON instead of text
15
19
 
16
- At least one of --prod and --dev is needed. A URL left out on a later run keeps
17
- the registered one.
20
+ At least one of --prod and --dev is needed with --new or when first registering.
21
+ A URL left out on a later run keeps the registered one; both may be omitted on
22
+ an update. Without --listed or --unlisted, re-registering keeps the current
23
+ listing setting.
18
24
 
19
25
  Needs HUB_URL and HUB_ADMIN_PASSWORD in the environment (.env and .env.local
20
26
  are read automatically). Running it again with the same
@@ -33,6 +39,8 @@ var { values, positionals } = parseArgs({
33
39
  hub: { type: "string" },
34
40
  json: { type: "boolean", default: false },
35
41
  new: { type: "boolean", default: false },
42
+ listed: { type: "boolean", default: false },
43
+ unlisted: { type: "boolean", default: false },
36
44
  help: { type: "boolean", short: "h", default: false }
37
45
  }
38
46
  });
@@ -47,10 +55,12 @@ if (!hub)
47
55
  fail("set HUB_URL (in .env or .env.local) or pass --hub");
48
56
  if (!password)
49
57
  fail("set HUB_ADMIN_PASSWORD (in .env or .env.local)");
50
- if (!values.prod && !values.dev)
51
- fail("pass --prod <url>, --dev <url> or both");
58
+ if (values.new && !values.prod && !values.dev)
59
+ fail("pass --prod <url>, --dev <url> or both when creating an app");
52
60
  if (!name)
53
61
  fail("pass --name <name>, the app's name in the hub");
62
+ if (values.listed && values.unlisted)
63
+ fail("pass --listed or --unlisted, not both");
54
64
  var response = await fetch(`${hub}/api/apps/register`, {
55
65
  method: "POST",
56
66
  headers: {
@@ -61,7 +71,9 @@ var response = await fetch(`${hub}/api/apps/register`, {
61
71
  name,
62
72
  ...values.prod !== undefined && { url: values.prod },
63
73
  ...values.dev !== undefined && { devUrl: values.dev },
64
- ...values.new && { createOnly: true }
74
+ ...values.new && { createOnly: true },
75
+ ...values.listed && { listed: true },
76
+ ...values.unlisted && { listed: false }
65
77
  })
66
78
  }).catch((error) => fail(`could not reach ${hub}: ${error.message}`));
67
79
  var result = await response.json().catch(() => ({}));
@@ -75,10 +87,16 @@ if (result.taken) {
75
87
  if (!response.ok) {
76
88
  fail(result.error ?? `the hub answered ${response.status}`);
77
89
  }
90
+ var wanted = values.listed ? true : values.unlisted ? false : undefined;
91
+ if (wanted !== undefined && result.listed !== wanted) {
92
+ fail(`"${name}" was registered, but the hub did not apply --${wanted ? "listed" : "unlisted"}: it needs updating to support the app list`);
93
+ }
78
94
  if (values.json) {
79
95
  console.log(JSON.stringify(result, null, 2));
80
96
  } else {
81
- console.log(`Registered "${name}" with key: ${result.key}
97
+ const listing = typeof result.listed === "boolean" ? `
98
+ Listed in the badge app list: ${result.listed ? "yes" : "no"}` : "";
99
+ console.log(`Registered "${name}" with key: ${result.key}${listing}
82
100
 
83
101
  Environment (Convex deployment, or the app's server):
84
102
  HUB_ISSUER=${result.env.HUB_ISSUER}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@user-hub/auth",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Sign people in from User Hub badges: Convex auth config, React provider, server verification and the hub-user CLI.",
5
5
  "type": "module",
6
6
  "files": [
@@ -5,7 +5,7 @@ description: Sign people into an app from their User Hub badge. Use when an app
5
5
 
6
6
  # User Hub auth
7
7
 
8
- People at a workshop scan a printed QR badge. The hub sends them to whichever app is live, signed in, with a token in the URL fragment (`#token=…`) that only that app accepts. 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. The hub sends them, signed in, to whichever app is live (or shows its 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.
9
9
 
10
10
  ## Starting a new app
11
11
 
@@ -40,7 +40,7 @@ The hub has one address, for example `https://hub.example.com`. The human gives
40
40
  You need four things. Ask for them; never guess.
41
41
 
42
42
  1. **The app's name** in the hub, unique to this app (the same name updates the same app, and apps copied from a template often share a package name).
43
- 2. **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, so badge scans never reach it; add it later by running register again with `--prod`.
43
+ 2. **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`.
44
44
  3. **Where `HUB_URL` and `HUB_ADMIN_PASSWORD` are.** You do not have the password, and you must never ask for it in chat, write it into a file yourself, commit it or print it. Ask the human which of these applies:
45
45
  - **`.env` or `.env.local`** in the app's repo (must be gitignored): the human adds `HUB_URL=…` and `HUB_ADMIN_PASSWORD=…` themselves. `bunx hub-user` reads `.env` and `.env.local` automatically.
46
46
  - **Infisical** (or another secret manager): run the command through it, for example `infisical run -- bunx hub-user register …`.
@@ -63,7 +63,9 @@ You need four things. Ask for them; never guess.
63
63
 
64
64
  `--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". Then ask the human for another name and run it again. Drop `--new` only when updating this app's own registration later.
65
65
 
66
- Leave out `--prod` if the app is not deployed yet. Running it again with the same `--name` updates the URLs; a URL left out keeps the registered one. If it fails with "set HUB_URL", "set HUB_ADMIN_PASSWORD" or a wrong password, stop and ask the human; do not work around it.
66
+ Leave out `--prod` if the app is not deployed yet. Running it again with the same `--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 name and saves the name as you typed it. Updating with no URL flags needs `@user-hub/auth` 0.2.1 or newer (`bun add @user-hub/auth@latest`); older versions ask for `--prod` or `--dev`. If it fails with "set HUB_URL", "set HUB_ADMIN_PASSWORD" or a wrong password, stop and ask the human; do not work around it.
67
+
68
+ New apps are **listed** in the badge app list by default. When the hub admin 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`. Both flags need `@user-hub/auth` 0.2.1 or newer (`bun add @user-hub/auth@latest`); older versions reject them. Pass neither when updating and the hub's current setting stays; the output shows it ("Listed in the badge app list: yes"). 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.
67
69
 
68
70
  It prints the app **key** (made from the name), `HUB_ISSUER` and `HUB_JWKS` (server side) and `VITE_HUB_URL` (frontend). Add `--json` to parse them.
69
71
 
@@ -127,7 +129,18 @@ Then:
127
129
 
128
130
  1. `bunx convex deploy`.
129
131
  2. On the host, set `VITE_CONVEX_URL` (the production deployment's URL) and `VITE_HUB_URL` (the hub's URL).
130
- 3. Tell the human the app is ready for the hub admin to schedule (or force live) under **Schedule**; until then badge scans go to whichever app is live.
132
+ 3. Tell the human the app is ready for the hub admin to schedule (or force live) under **Schedule**; until then badge scans go to whichever app is live. While **Badges go to** shows the app list, a listed app appears there as soon as it has its production URL.
133
+
134
+ ## Hub moved to a new URL
135
+
136
+ 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.
137
+
138
+ - **Starter apps** (they have `scripts/setup.ts`): ask the human to change `HUB_URL` in `.env.local`, 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.
139
+ - **Other apps:**
140
+ 1. Ask the human to change `HUB_URL` where they keep it (see question 3 above).
141
+ 2. Run `bunx hub-user register --name "<the app's name in the hub>"` without `--new` or any URL flags: it keeps the registered URLs and prints the new values. This needs `@user-hub/auth` 0.2.1 or newer: if it asks for `--prod` or `--dev`, run `bun add @user-hub/auth@latest` first. Use the exact name from the hub's **Apps** page: the CLI finds the app by the key made from the name. 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.
142
+ 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.
143
+ 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.
131
144
 
132
145
  ## Sign out
133
146
 
@@ -179,8 +192,8 @@ People are either `"member"` or `"facilitator"`, set in the hub. To give facilit
179
192
 
180
193
  ## Rules
181
194
 
182
- - Key all data on the hub id (`identity.subject` / `useHubUser().id`). Never on the name, and never on the badge token, which the app never sees.
195
+ - 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.
183
196
  - Check access on the server: `requireFacilitator(ctx)` / `requireHubUser(ctx)` in Convex, or `verifyHubToken` elsewhere. `useHubUser()` is for display only.
184
197
  - 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`.
185
198
  - Do not add other sign-in methods, sessions or token handling. If something is missing, change this package instead.
186
- - 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.
199
+ - 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. The same command updates 0.2.0, whose `hub-user` has no `--listed`/`--unlisted` and needs a URL flag on every run (both need 0.2.1 or newer).