@user-hub/auth 0.3.1 → 0.4.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 +18 -7
- package/dist/cli.js +131 -16
- package/package.json +1 -1
- package/skills/user-hub-auth/SKILL.md +25 -16
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 the app that is live, or shows
|
|
11
|
+
1. A person scans their badge. The hub sends them to the app that is live in their event (one hub runs many workshops, called events), or shows that event's 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`.
|
|
@@ -48,13 +48,22 @@ bunx hub-user login --hub https://hub.example.com
|
|
|
48
48
|
|
|
49
49
|
Paste an API key you created on the hub's **Account** page (`<hub>/account`; it shows as stars), or press Enter to sign in with your browser: it prints a link, you approve the request in the hub, and the CLI makes its own API key. Either way it checks the key with the hub and saves `HUB_URL` and `HUB_API_KEY` to the folder's `.env.local`, readable only by you, keeping the file's other lines. Keep `.env.local` gitignored. Without a terminal (scripts, coding agents), `bunx hub-user login --browser` prints the link for a person to approve, and `bunx hub-user login --api-key-stdin` reads a key from stdin. `bunx hub-user whoami` shows which account the folder is signed in as. Keys do not expire: delete one on `<hub>/account` to revoke it. Outside a folder with the package installed, run it as `bunx -p @user-hub/auth hub-user …`, never a bare `bunx hub-user`: there it fetches an npm package called `hub-user`, which is not ours. Hubs with accounts need `@user-hub/auth` 0.3 or newer. Upgrading from 0.2? Run `bun add @user-hub/auth@latest` (a `^0.2` range never installs 0.3), then `bunx hub-user login`; `HUB_ADMIN_PASSWORD` is no longer used, so delete it from your env files.
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
A hub runs many **events** (workshops); people, apps and the schedule each belong to one. Find the event your app is for by its slug, or create one (its time zone defaults to your machine's; pass `--time-zone Europe/Berlin` to pick another):
|
|
52
52
|
|
|
53
53
|
```sh
|
|
54
|
-
bunx hub-user
|
|
54
|
+
bunx hub-user events
|
|
55
|
+
bunx hub-user events create --name "Spring Workshop"
|
|
55
56
|
```
|
|
56
57
|
|
|
57
|
-
|
|
58
|
+
Hubs with events need `@user-hub/auth` 0.4 or newer. Upgrading from 0.3? Run `bun add @user-hub/auth@latest` first (a `^0.3` range never installs 0.4); no app code changes.
|
|
59
|
+
|
|
60
|
+
Then register the app in that event (`bunx` reads `.env` and `.env.local` automatically; if `HUB_URL` and `HUB_API_KEY` live in a secret manager, run it through that):
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
bunx hub-user register --event spring-workshop --name "Your App" --new --prod https://your-app.com --dev http://localhost:3000
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`--event` is needed on every run; set `HUB_EVENT=<slug>` in `.env.local` to leave it out. An unknown slug exits with code 5. It prints your **app key** (made from the event's slug and 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 `--event` and `--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. Any hub account can update any app, so register only under your app's own name. (The same can be done under **Apps** in the hub, with the values under **Setup**.) Quote a value with spaces (`--name "Summer Camp"`): an unexpected extra word stops any `hub-user` command with exit code 1 before it sends anything. When you pass `--hub`, the commands the CLI suggests next keep it.
|
|
58
67
|
|
|
59
68
|
### 3. Set the values
|
|
60
69
|
|
|
@@ -118,7 +127,7 @@ export const myVotes = query({
|
|
|
118
127
|
Run your dev server as usual and open it. With no token yet, it sends you to the hub, where you pick who to be (sign in to the hub with your account once per browser), and brings you back signed in. You stay signed in across reloads and restarts until that person's expiration in the hub (4 hours unless set otherwise).
|
|
119
128
|
|
|
120
129
|
- **Switch person:** open `<hub>/open/<your-app-key>`.
|
|
121
|
-
- **Different port:** run `hub-user register` again with the new `--dev` URL. The hub only sends tokens to registered URLs.
|
|
130
|
+
- **Different port:** run `hub-user register` again (same `--event` and `--name`) with the new `--dev` URL. The hub only sends tokens to registered URLs.
|
|
122
131
|
- **Production:** someone without a token sees "Scan your badge to sign in". Pass `signedOut={…}` to `<HubSignedIn>` to style that screen.
|
|
123
132
|
|
|
124
133
|
## Sign out
|
|
@@ -186,8 +195,10 @@ Apps without Convex use `HubAuthProvider` and `HubSignedIn` from `@user-hub/auth
|
|
|
186
195
|
| `@user-hub/auth/server` | `verifyHubToken(requestOrToken, { app })` | Checks a token anywhere else |
|
|
187
196
|
| CLI | `hub-user login [--hub <url>] [--browser\|--api-key-stdin]` | Signs the folder in: an API key pasted (hidden) or from stdin, or a browser sign-in it prints a link for (it never opens a browser). Saves `HUB_URL` and `HUB_API_KEY` to `./.env.local` (mode 600) |
|
|
188
197
|
| | `hub-user whoami [--json]` | The account `HUB_API_KEY` belongs to; exit code 4 when not signed in |
|
|
189
|
-
| | `hub-user
|
|
190
|
-
| |
|
|
198
|
+
| | `hub-user events [--json]` | The hub's events, newest first: slug, name, time zone, ended ones marked. `--json` prints the array of `{ slug, name, status, timeZone }` |
|
|
199
|
+
| | `hub-user events create --name <name> [--time-zone <zone>] [--json]` | Creates an event (the IANA time zone defaults to this machine's); exit code 3 if its slug is taken. `--json` prints the created event |
|
|
200
|
+
| | `hub-user register --event <slug> --name <name> [--prod <url>] [--dev <url>] [--listed\|--unlisted] [--new] [--hub <url>] [--json]` | Registers or updates the app in an event (`--event` falls back to `HUB_EVENT`; exit code 5 when no event has that slug; 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 (also without `--new` when the key belongs to an app of another event); `--json` includes `event: { slug, name }`; `--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) |
|
|
201
|
+
| | Exit codes | 0 done, 1 error (an unexpected extra word, such as an unquoted `--name Summer Camp`, stops before any request), 3 name taken (`register`, `events create`), 4 not signed in (run `hub-user login`), 5 unknown event (`register`) |
|
|
191
202
|
|
|
192
203
|
## Developing this package
|
|
193
204
|
|
package/dist/cli.js
CHANGED
|
@@ -9,7 +9,9 @@ import { parseArgs } from "node:util";
|
|
|
9
9
|
var USAGE = `Usage:
|
|
10
10
|
hub-user login [--hub <url>] [--browser | --api-key-stdin]
|
|
11
11
|
hub-user whoami [--hub <url>] [--json]
|
|
12
|
-
hub-user
|
|
12
|
+
hub-user events [--hub <url>] [--json]
|
|
13
|
+
hub-user events create --name <name> [--time-zone <zone>] [--hub <url>] [--json]
|
|
14
|
+
hub-user register --event <slug> --name <name> [--prod <url>] [--dev <url>] [--listed|--unlisted] [--new] [--hub <url>] [--json]
|
|
13
15
|
|
|
14
16
|
login Signs this folder in to the hub: saves HUB_URL and HUB_API_KEY to
|
|
15
17
|
./.env.local (mode 600, other lines kept; keep it out of git).
|
|
@@ -22,8 +24,18 @@ login Signs this folder in to the hub: saves HUB_URL and HUB_API_KEY to
|
|
|
22
24
|
|
|
23
25
|
whoami Shows the hub account HUB_API_KEY belongs to.
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
events Lists the hub's events (workshops), newest first, with the slug
|
|
28
|
+
that register takes.
|
|
29
|
+
create Creates an event:
|
|
30
|
+
--name The event's name; its slug comes from it (required)
|
|
31
|
+
--time-zone An IANA time zone such as Europe/Berlin (default: this
|
|
32
|
+
machine's)
|
|
33
|
+
|
|
34
|
+
register Registers the app in an event, or updates it when the name exists
|
|
35
|
+
there.
|
|
36
|
+
--event The event's slug (default: HUB_EVENT); see hub-user events
|
|
37
|
+
--name The app's name in the hub; its key comes from the event and the
|
|
38
|
+
name (required)
|
|
27
39
|
--prod Where badge scans send people. Leave it out until the app is
|
|
28
40
|
deployed: it can be developed, but not scheduled or shown in
|
|
29
41
|
the badge app list.
|
|
@@ -39,16 +51,19 @@ register Registers the app, or updates it when the name exists.
|
|
|
39
51
|
At least one of --prod and --dev is needed with --new or when first registering.
|
|
40
52
|
A URL left out on a later run keeps the registered one; both may be omitted on
|
|
41
53
|
an update. Without --listed or --unlisted, re-registering keeps the current
|
|
42
|
-
listing setting. Running it again with the same name updates that
|
|
54
|
+
listing setting. Running it again with the same event and name updates that
|
|
55
|
+
app.
|
|
43
56
|
|
|
44
|
-
whoami and register need HUB_URL and HUB_API_KEY in the environment
|
|
45
|
-
.env.local are read automatically); hub-user login puts them there.
|
|
57
|
+
whoami, events and register need HUB_URL and HUB_API_KEY in the environment
|
|
58
|
+
(.env and .env.local are read automatically); hub-user login puts them there.
|
|
46
59
|
|
|
47
|
-
Exit codes: 0 done, 1 error, 3 name taken (register --new
|
|
60
|
+
Exit codes: 0 done, 1 error, 3 name taken (register --new, an app of another
|
|
61
|
+
event, events create), 4 not signed in, 5 unknown event (register).`;
|
|
48
62
|
var ENV_FILE = ".env.local";
|
|
49
63
|
var CLIENT_ID = "hub-user";
|
|
50
64
|
var DEVICE_GRANT = "urn:ietf:params:oauth:grant-type:device_code";
|
|
51
|
-
var
|
|
65
|
+
var CLI = "bunx -p @user-hub/auth hub-user";
|
|
66
|
+
var LOGIN = `${CLI} login`;
|
|
52
67
|
var KEY_NAME_LIMIT = 128;
|
|
53
68
|
function fail(message) {
|
|
54
69
|
console.error(`hub-user: ${message}`);
|
|
@@ -69,6 +84,8 @@ function parse() {
|
|
|
69
84
|
prod: { type: "string" },
|
|
70
85
|
dev: { type: "string" },
|
|
71
86
|
name: { type: "string" },
|
|
87
|
+
event: { type: "string" },
|
|
88
|
+
"time-zone": { type: "string" },
|
|
72
89
|
hub: { type: "string" },
|
|
73
90
|
json: { type: "boolean", default: false },
|
|
74
91
|
new: { type: "boolean", default: false },
|
|
@@ -87,10 +104,21 @@ ${USAGE}`);
|
|
|
87
104
|
}
|
|
88
105
|
var { values, positionals } = parse();
|
|
89
106
|
var command = positionals[0];
|
|
90
|
-
|
|
107
|
+
var subcommand = positionals[1];
|
|
108
|
+
if (values.help || !command || !["login", "whoami", "events", "register"].includes(command) || command === "events" && subcommand !== undefined && subcommand !== "create") {
|
|
91
109
|
console.log(USAGE);
|
|
92
110
|
process.exit(values.help ? 0 : 1);
|
|
93
111
|
}
|
|
112
|
+
var extra = positionals.slice(command === "events" && subcommand === "create" ? 2 : 1);
|
|
113
|
+
if (extra.length > 0) {
|
|
114
|
+
fail(`unexpected argument${extra.length > 1 ? "s" : ""} ${extra.map((word) => `"${word}"`).join(" ")}: quote a value with spaces, like --name "Summer Camp"`);
|
|
115
|
+
}
|
|
116
|
+
function hubFlag() {
|
|
117
|
+
const hub = values.hub?.trim().replace(/\/+$/, "");
|
|
118
|
+
if (!hub)
|
|
119
|
+
return "";
|
|
120
|
+
return ` --hub ${/^[\w@%+=:,./-]+$/.test(hub) ? hub : `'${hub.replaceAll("'", "'\\''")}'`}`;
|
|
121
|
+
}
|
|
94
122
|
var oldPassword = Boolean(process.env.HUB_ADMIN_PASSWORD && !process.env.HUB_API_KEY?.trim());
|
|
95
123
|
if (oldPassword && command === "login") {
|
|
96
124
|
console.error("hub-user: HUB_ADMIN_PASSWORD is no longer used; delete it from your env files.");
|
|
@@ -324,12 +352,77 @@ Saved HUB_URL and HUB_API_KEY to ${ENV_FILE}.`);
|
|
|
324
352
|
async function whoami() {
|
|
325
353
|
const { hub, key } = credentials(values.json);
|
|
326
354
|
const me = await account(hub, key);
|
|
327
|
-
if (!me)
|
|
328
|
-
|
|
329
|
-
}
|
|
355
|
+
if (!me)
|
|
356
|
+
refused(hub);
|
|
330
357
|
console.log(values.json ? JSON.stringify({ hub, ...me }, null, 2) : `${signedIn(hub, me)}${me.name && me.name !== me.username ? `
|
|
331
358
|
Name: ${me.name}` : ""}`);
|
|
332
359
|
}
|
|
360
|
+
function refused(hub) {
|
|
361
|
+
denied(`${hub} did not accept HUB_API_KEY (deleted, or for another hub): run \`${LOGIN}\``, values.json ? { status: 401 } : undefined);
|
|
362
|
+
}
|
|
363
|
+
function noEvents(hub) {
|
|
364
|
+
fail(`${hub} has no events: the hub needs updating`);
|
|
365
|
+
}
|
|
366
|
+
async function listEvents() {
|
|
367
|
+
const { hub, key } = credentials(values.json);
|
|
368
|
+
const { status, ok, body } = await call(hub, "/api/events", { token: key });
|
|
369
|
+
if (status === 401)
|
|
370
|
+
refused(hub);
|
|
371
|
+
if (status === 404)
|
|
372
|
+
noEvents(hub);
|
|
373
|
+
if (!ok || !Array.isArray(body.events)) {
|
|
374
|
+
fail(typeof body.error === "string" ? body.error : `the hub answered ${status}`);
|
|
375
|
+
}
|
|
376
|
+
const events = body.events;
|
|
377
|
+
if (values.json) {
|
|
378
|
+
console.log(JSON.stringify(events, null, 2));
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
if (events.length === 0) {
|
|
382
|
+
console.log(`No events yet. Create one with \`${CLI} events create --name <name>${hubFlag()}\`.`);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
const width = Math.max(...events.map((event) => event.slug.length));
|
|
386
|
+
console.log(events.map((event) => `${event.slug.padEnd(width)} ${event.name} (${event.timeZone}${event.status === "ended" ? ", ended" : ""})`).join(`
|
|
387
|
+
`));
|
|
388
|
+
}
|
|
389
|
+
async function createEvent() {
|
|
390
|
+
const name = values.name?.trim();
|
|
391
|
+
if (!name)
|
|
392
|
+
fail("pass --name <name>, the event's name");
|
|
393
|
+
if (values["time-zone"] !== undefined && !values["time-zone"].trim()) {
|
|
394
|
+
fail("--time-zone needs a zone such as Europe/Berlin: leave it out to use this machine's");
|
|
395
|
+
}
|
|
396
|
+
const timeZone = values["time-zone"]?.trim() ?? Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
397
|
+
const { hub, key } = credentials(values.json);
|
|
398
|
+
const { status, ok, body } = await call(hub, "/api/events", {
|
|
399
|
+
token: key,
|
|
400
|
+
body: { name, timeZone }
|
|
401
|
+
});
|
|
402
|
+
if (status === 409 && body.taken) {
|
|
403
|
+
if (values.json)
|
|
404
|
+
console.log(JSON.stringify(body, null, 2));
|
|
405
|
+
else
|
|
406
|
+
console.error(`hub-user: ${body.error}`);
|
|
407
|
+
process.exit(3);
|
|
408
|
+
}
|
|
409
|
+
if (status === 401)
|
|
410
|
+
refused(hub);
|
|
411
|
+
if (status === 404)
|
|
412
|
+
noEvents(hub);
|
|
413
|
+
if (!ok || typeof body.slug !== "string") {
|
|
414
|
+
fail(typeof body.error === "string" ? body.error : `the hub answered ${status}`);
|
|
415
|
+
}
|
|
416
|
+
const event = body;
|
|
417
|
+
if (values.json) {
|
|
418
|
+
console.log(JSON.stringify(event, null, 2));
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
console.log(`Created event "${event.name}" with slug: ${event.slug} (time zone ${event.timeZone})
|
|
422
|
+
|
|
423
|
+
Register apps in it with:
|
|
424
|
+
${CLI} register --event ${event.slug} --name <app name>${hubFlag()} …`);
|
|
425
|
+
}
|
|
333
426
|
async function register() {
|
|
334
427
|
const name = values.name?.trim();
|
|
335
428
|
for (const flag of ["prod", "dev"]) {
|
|
@@ -343,10 +436,18 @@ async function register() {
|
|
|
343
436
|
fail("pass --name <name>, the app's name in the hub");
|
|
344
437
|
if (values.listed && values.unlisted)
|
|
345
438
|
fail("pass --listed or --unlisted, not both");
|
|
439
|
+
if (values.event !== undefined && !values.event.trim()) {
|
|
440
|
+
fail(`--event needs an event's slug: see \`${CLI} events${hubFlag()}\``);
|
|
441
|
+
}
|
|
442
|
+
const event = (values.event ?? process.env.HUB_EVENT)?.trim();
|
|
443
|
+
if (!event) {
|
|
444
|
+
fail(`pass --event <slug> or set HUB_EVENT to choose the hub event the app belongs to: \`${CLI} events${hubFlag()}\` lists them and \`${CLI} events create --name <name>${hubFlag()}\` creates one`);
|
|
445
|
+
}
|
|
346
446
|
const { hub, key } = credentials(values.json);
|
|
347
447
|
const { status, ok, body } = await call(hub, "/api/apps/register", {
|
|
348
448
|
token: key,
|
|
349
449
|
body: {
|
|
450
|
+
event,
|
|
350
451
|
name,
|
|
351
452
|
...values.prod !== undefined && { url: values.prod },
|
|
352
453
|
...values.dev !== undefined && { devUrl: values.dev },
|
|
@@ -363,12 +464,21 @@ async function register() {
|
|
|
363
464
|
console.error(`hub-user: ${result.error}`);
|
|
364
465
|
process.exit(3);
|
|
365
466
|
}
|
|
366
|
-
if (status ===
|
|
367
|
-
|
|
467
|
+
if (status === 404 && result.unknownEvent) {
|
|
468
|
+
if (values.json)
|
|
469
|
+
console.log(JSON.stringify(result, null, 2));
|
|
470
|
+
else
|
|
471
|
+
console.error(`hub-user: ${result.error ?? `no event has the slug "${event}"`} (\`${CLI} events${hubFlag()}\` lists them)`);
|
|
472
|
+
process.exit(5);
|
|
368
473
|
}
|
|
474
|
+
if (status === 401)
|
|
475
|
+
refused(hub);
|
|
369
476
|
if (!ok) {
|
|
370
477
|
fail(result.error ?? `the hub answered ${status}`);
|
|
371
478
|
}
|
|
479
|
+
if (!result.event) {
|
|
480
|
+
fail(`"${name}" was registered, but not in the event "${event}": the hub needs updating to support events`);
|
|
481
|
+
}
|
|
372
482
|
const wanted = values.listed ? true : values.unlisted ? false : undefined;
|
|
373
483
|
if (wanted !== undefined && result.listed !== wanted) {
|
|
374
484
|
fail(`"${name}" was registered, but the hub did not apply --${wanted ? "listed" : "unlisted"}: it needs updating to support the app list`);
|
|
@@ -379,7 +489,7 @@ async function register() {
|
|
|
379
489
|
}
|
|
380
490
|
const listing = typeof result.listed === "boolean" ? `
|
|
381
491
|
Listed in the badge app list: ${result.listed ? values.prod ? "yes" : "yes, once it has a production URL" : "no"}` : "";
|
|
382
|
-
console.log(`Registered "${name}" with key: ${result.key}${listing}
|
|
492
|
+
console.log(`Registered "${name}" in the event "${result.event.name}" (${result.event.slug}) with key: ${result.key}${listing}
|
|
383
493
|
|
|
384
494
|
Environment (Convex deployment, or the app's server):
|
|
385
495
|
HUB_ISSUER=${result.env.HUB_ISSUER}
|
|
@@ -398,5 +508,10 @@ if (command === "login")
|
|
|
398
508
|
await login();
|
|
399
509
|
else if (command === "whoami")
|
|
400
510
|
await whoami();
|
|
401
|
-
else
|
|
511
|
+
else if (command === "events") {
|
|
512
|
+
if (subcommand === "create")
|
|
513
|
+
await createEvent();
|
|
514
|
+
else
|
|
515
|
+
await listEvents();
|
|
516
|
+
} else
|
|
402
517
|
await register();
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
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 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 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.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# User Hub auth
|
|
7
7
|
|
|
8
|
-
People at a workshop scan a printed QR badge. The hub sends them, signed in, to whichever app is live (or shows
|
|
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.
|
|
9
9
|
|
|
10
10
|
## Starting a new app
|
|
11
11
|
|
|
@@ -25,6 +25,8 @@ bun run setup # Convex project, hub registration, HUB_ISSUER and HUB_JWKS, on
|
|
|
25
25
|
bun run dev # http://localhost:3000
|
|
26
26
|
```
|
|
27
27
|
|
|
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>`.
|
|
29
|
+
|
|
28
30
|
That covers every step below; read on only to add User Hub to an existing app or to change the wiring.
|
|
29
31
|
|
|
30
32
|
## The hub's URL
|
|
@@ -37,12 +39,13 @@ The hub has one address, for example `https://hub.example.com`. The human gives
|
|
|
37
39
|
|
|
38
40
|
## Before you start: ask the human
|
|
39
41
|
|
|
40
|
-
You need
|
|
42
|
+
You need five things. Ask for them; never guess.
|
|
41
43
|
|
|
42
|
-
1. **
|
|
43
|
-
2. **
|
|
44
|
-
3. **
|
|
45
|
-
4. **
|
|
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.
|
|
46
49
|
|
|
47
50
|
## Signing in
|
|
48
51
|
|
|
@@ -55,6 +58,8 @@ Sign the folder in one of two ways, from the app's folder:
|
|
|
55
58
|
|
|
56
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.
|
|
57
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
|
+
|
|
58
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.
|
|
59
64
|
|
|
60
65
|
## Steps
|
|
@@ -65,19 +70,23 @@ The hub's URL comes from the human (or `HUB_URL` already in `.env.local`, which
|
|
|
65
70
|
bun add @user-hub/auth
|
|
66
71
|
```
|
|
67
72
|
|
|
68
|
-
2. **Register the app** from the app's repo root, once the folder is signed in (question
|
|
73
|
+
2. **Register the app** in its event from the app's repo root, once the folder is signed in (question 4 above):
|
|
69
74
|
|
|
70
75
|
```sh
|
|
71
|
-
bunx -p @user-hub/auth hub-user register --name "<app name>" --new --prod https://app.example.com --dev http://localhost:3000
|
|
76
|
+
bunx -p @user-hub/auth hub-user register --event <slug> --name "<app name>" --new --prod https://app.example.com --dev http://localhost:3000
|
|
72
77
|
```
|
|
73
78
|
|
|
74
|
-
`--
|
|
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.
|
|
75
84
|
|
|
76
|
-
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. 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.
|
|
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.
|
|
77
86
|
|
|
78
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.
|
|
79
88
|
|
|
80
|
-
It prints the app **key** (
|
|
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 }`).
|
|
81
90
|
|
|
82
91
|
3. **Set the two values** where tokens are checked:
|
|
83
92
|
- Convex: `bunx convex env set HUB_ISSUER '…'` and `bunx convex env set HUB_JWKS '…'` (on every deployment the app uses).
|
|
@@ -125,7 +134,7 @@ The hub's URL comes from the human (or `HUB_URL` already in `.env.local`, which
|
|
|
125
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.
|
|
126
135
|
|
|
127
136
|
- To switch person: open `<hub>/open/<key>` and pick someone else.
|
|
128
|
-
- If the dev server runs on another port, run `hub-user register` again with the same `--name` and the new `--dev` URL: the hub only sends tokens back to registered URLs.
|
|
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.
|
|
129
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".
|
|
130
139
|
|
|
131
140
|
## Production URL
|
|
@@ -133,13 +142,13 @@ Run the app as usual (`bun run dev`) on the `--dev` URL you registered. Opening
|
|
|
133
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:
|
|
134
143
|
|
|
135
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.
|
|
136
|
-
- **Other apps:** `bunx -p @user-hub/auth hub-user register --name "<the app's name in the hub>" --prod https://<deployed url>`, without `--new` (this updates the app's own registration; the
|
|
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 '…'`.
|
|
137
146
|
|
|
138
147
|
Then:
|
|
139
148
|
|
|
140
149
|
1. `bunx convex deploy`.
|
|
141
150
|
2. On the host, set `VITE_CONVEX_URL` (the production deployment's URL) and `VITE_HUB_URL` (the hub's URL).
|
|
142
|
-
3. Tell the human the app is ready to be scheduled (or forced live) under **Schedule** in the hub; 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.
|
|
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.
|
|
143
152
|
|
|
144
153
|
## Hub moved to a new URL
|
|
145
154
|
|
|
@@ -148,7 +157,7 @@ When the human says the hub's URL changed, hub ids stay the same, so data keyed
|
|
|
148
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.
|
|
149
158
|
- **Other apps:**
|
|
150
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.
|
|
151
|
-
2. Run `bunx -p @user-hub/auth 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. Use the exact name from the
|
|
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.
|
|
152
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.
|
|
153
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.
|
|
154
163
|
|