@user-hub/auth 0.7.1 → 0.7.3
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 +7 -2
- package/package.json +1 -1
- package/skills/user-hub-auth/SKILL.md +4 -3
- package/skills/user-hub-auth/references/connect-an-app.md +2 -2
- package/skills/user-hub-auth/references/dev-and-production.md +8 -5
- package/skills/user-hub-auth/references/roles.md +14 -1
- package/skills/user-hub-auth/references/server-functions.md +14 -0
package/README.md
CHANGED
|
@@ -49,7 +49,7 @@ bunx user-hub events list
|
|
|
49
49
|
bunx user-hub apps register --event spring-workshop --name "Your App" --new --prod https://your-app.com --dev http://localhost:3000
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
`login` saves `HUB_URL` and `HUB_API_KEY` to the folder's `.env.local` (keep it gitignored). A hub runs many **events** (workshops), and an app belongs to one. `apps register` prints your **app key** (made from the event's slug and the name) and the values to set. Not deployed yet? Leave out `--prod`; add it later
|
|
52
|
+
`login` saves `HUB_URL` and `HUB_API_KEY` to the folder's `.env.local` (keep it gitignored). A hub runs many **events** (workshops), and an app belongs to one. `apps register` prints your **app key** (made from the event's slug and the name) and the values to set. Not deployed yet? Leave out `--prod`; add it later with `bunx user-hub prod set https://your-app.com --event spring-workshop --app "Your App"`. An app has a list of development URLs: add more with `bunx user-hub dev add <url> --event spring-workshop --app "Your App"`. `--new` refuses a name another app already has. See `bunx user-hub --help` for everything else, including the event's people. (The same can be done under **Apps** in the hub, with the values under **Setup**.) A new app made with `bun create user-hub@latest` does all of this with `bun run setup`.
|
|
53
53
|
|
|
54
54
|
### 3. Set the values
|
|
55
55
|
|
|
@@ -153,6 +153,9 @@ export const saveSummary = createServerFn({ method: "POST" })
|
|
|
153
153
|
- `context.convex` is a `ConvexHttpClient` acting as the person, so Convex functions still check them with `requireHubUser`. Without Convex, use `hubMiddleware({ app, hubUrl })` from `@user-hub/auth/start-middleware`: the same, with `context.hub` and `context.hubToken`.
|
|
154
154
|
- Tokens are checked against `HUB_ISSUER` and `HUB_JWKS` when the server has them, else against `hubUrl` and its published key, so a container with no runtime environment works. Pass both URLs from `import.meta.env`, never `process.env.VITE_*`.
|
|
155
155
|
- Start protects server functions from cross-site calls by default; an app that defines `src/start.ts` adds `createCsrfMiddleware` itself.
|
|
156
|
+
- The browser half needs the provider (`ConvexHubAuthProvider` or `HubAuthProvider`) mounted, as every connected app has; without one, calls wait 10 seconds and then fail.
|
|
157
|
+
- Roles: check `context.hub.role` in the handler (and keep `requireFacilitator` in Convex).
|
|
158
|
+
- Replacing your own version (a hook that adds the header, a middleware calling `verifyHubToken` and `ConvexHttpClient.setAuth`)? See "Moving from a hand-rolled version" in the skill's `references/server-functions.md`.
|
|
156
159
|
|
|
157
160
|
### 5. Use the person
|
|
158
161
|
|
|
@@ -179,7 +182,7 @@ export const myVotes = query({
|
|
|
179
182
|
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).
|
|
180
183
|
|
|
181
184
|
- **Switch person:** open `<hub>/open/<your-app-key>`.
|
|
182
|
-
- **Different port:**
|
|
185
|
+
- **Different port:** add it to the app's development URLs: `user-hub dev add http://localhost:<port>` (with `--event` and `--app` outside a starter app). The hub only sends tokens to registered URLs.
|
|
183
186
|
- **Production:** someone without a token sees "Scan your badge to sign in". Pass `signedOut={…}` to `<HubSignedIn>` to style that screen.
|
|
184
187
|
|
|
185
188
|
## Sign out
|
|
@@ -214,6 +217,8 @@ const me = useHubUser();
|
|
|
214
217
|
if (me?.role !== "facilitator") return <p>This page is for facilitators.</p>;
|
|
215
218
|
```
|
|
216
219
|
|
|
220
|
+
In TanStack Start server functions and server routes behind the middleware, check `context.hub.role` the same way.
|
|
221
|
+
|
|
217
222
|
Hiding a page does not protect it: the server check does.
|
|
218
223
|
|
|
219
224
|
## Not using Convex?
|
package/package.json
CHANGED
|
@@ -16,10 +16,11 @@ Registering apps, signing a folder in to the hub and managing people is done wit
|
|
|
16
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
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
18
|
| Server functions or server routes that act as the person (TanStack Start) | [references/server-functions.md](references/server-functions.md) |
|
|
19
|
+
| An app that checks tokens itself in server code (a header-adding hook, `verifyHubToken` plus `ConvexHttpClient`) | Replace it: "Moving from a hand-rolled version" in [references/server-functions.md](references/server-functions.md) |
|
|
19
20
|
| Facilitator-only pages or actions | [references/roles.md](references/roles.md) |
|
|
20
21
|
| Running it locally, going to production, or the hub moved | [references/dev-and-production.md](references/dev-and-production.md) |
|
|
21
22
|
|
|
22
|
-
A starter app is one whose `package.json` has `"setup": "user-hub setup"`: for it, prefer `bun run setup` (with `--event
|
|
23
|
+
A starter app is one whose `package.json` has `"setup": "user-hub setup"`: for it, prefer `bun run setup` (with `--event`) over doing the steps by hand, and `user-hub dev` / `user-hub prod` for its development URLs and production URL.
|
|
23
24
|
|
|
24
25
|
## Ask the human first
|
|
25
26
|
|
|
@@ -27,7 +28,7 @@ Never guess these; ask:
|
|
|
27
28
|
|
|
28
29
|
1. **Which event** the app is for (the CLI lists them). Create an event only when the human asks.
|
|
29
30
|
2. **The app's name** in the hub, unique within the event.
|
|
30
|
-
3. **Its local
|
|
31
|
+
3. **Its local URLs** (usually `http://localhost:3000`; an app has a list of development URLs) and its **production URL** once deployed.
|
|
31
32
|
4. **Which Convex deployments** it uses (dev, prod), so the values go on each.
|
|
32
33
|
|
|
33
34
|
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.
|
|
@@ -38,6 +39,6 @@ The hub's URL also comes from the human. It goes in three places with the same v
|
|
|
38
39
|
- **Check access on the server**: `requireHubUser(ctx)` / `requireFacilitator(ctx)` in Convex, `hub.serverFn` / `hub.request` middleware on TanStack Start server functions and routes ([references/server-functions.md](references/server-functions.md)), `verifyHubToken` elsewhere. `useHubUser()` is for display; hiding a page protects nothing.
|
|
39
40
|
- **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.
|
|
40
41
|
- **Server rendering (TanStack Start):** pass `getHubToken()` from `@user-hub/auth/start` to the provider as `initialToken` from the root route's `beforeLoad` ([references/connect-an-app.md](references/connect-an-app.md)), so a page load renders the person instead of `loading`. Everything inside `<HubSignedIn>` then renders on the server: keep `window` and `document` inside effects. With Convex, create the client with `expectAuth: true` and `initialAuthTokenReuse: true`.
|
|
41
|
-
- **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.
|
|
42
|
+
- **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. Server functions behind `hub.serverFn` get it by themselves (no headers, no wrapper hook); for anything else read `useHubToken()` for each request.
|
|
42
43
|
- **No other sign-in.** No login screens, passwords or sessions. If something is missing, it belongs in `@user-hub/auth`.
|
|
43
44
|
- **Secrets stay out of chat and files you write.** The API key lives only in `.env.local`, saved by `user-hub login`.
|
|
@@ -18,7 +18,7 @@ bunx user-hub apps register --event <slug> --name "<app name>" --new --dev http:
|
|
|
18
18
|
```
|
|
19
19
|
|
|
20
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
|
|
21
|
+
- Leave out `--prod` until the app is deployed; then set it with `bunx user-hub prod set <url> --event <slug> --app "<app name>"`. An app has a list of development URLs: add others with `bunx user-hub dev add <url> --event <slug> --app "<app name>"`.
|
|
22
22
|
- It prints the app **key** (`<event slug>-<name>`), `HUB_ISSUER`, `HUB_JWKS` and `VITE_HUB_URL`. With `--json`: `key`, `env`, `hubUrl`.
|
|
23
23
|
|
|
24
24
|
## 3. Set the values
|
|
@@ -75,7 +75,7 @@ const { hubToken } = Route.useRouteContext();
|
|
|
75
75
|
|
|
76
76
|
With `ConvexQueryClient` (Convex through TanStack Query), also call `context.convexQueryClient.serverHttpClient?.setAuth(hubToken)` in `beforeLoad` when there is a token, so server-side queries run as the person.
|
|
77
77
|
|
|
78
|
-
With Convex, create the client so queries wait for the person's token and keep it, instead of running signed out first and then signing in twice: `new ConvexReactClient(url, { expectAuth: true, initialAuthTokenReuse: true })`, or the same options as the second argument of `new ConvexQueryClient(url, …)`. On Vite, also pre-bundle Convex and this package together, so the browser holds one `convex/react` ("Could not find Convex client" otherwise): `optimizeDeps: { include: ["convex/react", "@user-hub/auth/convex-react"] }`, plus `"@convex-dev/react-query"` when you use it. `ConvexHubAuthProvider` reloads the page when the person signs out (here, in another tab, or by the hub), as Convex recommends with `expectAuth`: the new page starts signed out, so no cached query runs again without the token. Never read or set the `user-hub-token.<key>` cookie yourself: the provider writes it and `getHubToken`
|
|
78
|
+
With Convex, create the client so queries wait for the person's token and keep it, instead of running signed out first and then signing in twice: `new ConvexReactClient(url, { expectAuth: true, initialAuthTokenReuse: true })`, or the same options as the second argument of `new ConvexQueryClient(url, …)`. On Vite, also pre-bundle Convex and this package together, so the browser holds one `convex/react` ("Could not find Convex client" otherwise): `optimizeDeps: { include: ["convex/react", "@user-hub/auth/convex-react"] }`, plus `"@convex-dev/react-query"` when you use it. `ConvexHubAuthProvider` reloads the page when the person signs out (here, in another tab, or by the hub), as Convex recommends with `expectAuth`: the new page starts signed out, so no cached query runs again without the token. Never read or set the `user-hub-token.<key>` cookie yourself: the provider writes it, and `getHubToken` and the server-function middleware read it.
|
|
79
79
|
|
|
80
80
|
## 5. Use the person
|
|
81
81
|
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
## Your dev server
|
|
4
4
|
|
|
5
|
-
Run the app on the development
|
|
5
|
+
Run the app on one of the development URLs registered with the hub (an app has a list of them). 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
6
|
|
|
7
7
|
- **Switch person:** open `<hub>/open/<key>`.
|
|
8
|
-
- **Another port:** the hub only sends tokens to registered URLs. Starter apps: `
|
|
8
|
+
- **Another port:** the hub only sends tokens to registered URLs. Starter apps: `bunx user-hub dev add http://localhost:<port>` and change the port in the `dev` script too. Others: the same with `--event <slug> --app "<the app's name>"`. `bunx user-hub dev list` shows them, `dev remove <url>` drops an old one.
|
|
9
9
|
- **Test people:** `bunx user-hub people create --name "Test Person" --team Test` (user-hub-cli skill).
|
|
10
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
11
|
|
|
@@ -13,10 +13,12 @@ Run the app on the development URL registered with the hub. With no token, it se
|
|
|
13
13
|
|
|
14
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
15
|
|
|
16
|
-
- **Starter apps:** `
|
|
17
|
-
- **Other apps:** `bunx user-hub
|
|
16
|
+
- **Starter apps:** `bunx user-hub prod set https://<deployed url>`. It also sets `HUB_ISSUER` and `HUB_JWKS` on the production Convex deployment.
|
|
17
|
+
- **Other apps:** `bunx user-hub prod set https://<deployed url> --event <slug> --app "<the app's name>"`, then `bunx convex env set --prod HUB_ISSUER '…'` and `… HUB_JWKS '…'` (`bunx user-hub apps register --event <slug> --name "<the app's name>"` without `--new` prints them).
|
|
18
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.
|
|
19
|
+
Then `bunx convex deploy`, set `VITE_CONVEX_URL` and `VITE_HUB_URL` on the host (at build time: Vite builds them in), 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
|
+
Server functions and server routes behind `hub.serverFn` / `hub.request` need nothing more on the host: they check tokens against the built-in `VITE_HUB_URL` and the hub's published key. `HUB_ISSUER` and `HUB_JWKS` in the host's runtime environment are optional; when set, they win, so keep them right (see below).
|
|
20
22
|
|
|
21
23
|
## The hub moved to a new URL
|
|
22
24
|
|
|
@@ -28,3 +30,4 @@ Hub ids stay the same, so data keyed on the id carries over, but the app rejects
|
|
|
28
30
|
- `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
31
|
- Set `HUB_ISSUER` and `HUB_JWKS` on every Convex deployment (with and without `--prod`). No deploy needed for that.
|
|
30
32
|
- Set `VITE_HUB_URL` to the new URL, then rebuild and redeploy the frontend.
|
|
33
|
+
4. **Any app whose server sets `HUB_ISSUER` or `HUB_JWKS` in its own runtime environment** (for `verifyHubToken` or the server-function middleware): update them there too, or every server call rejects sign-ins. Starter apps set none.
|
|
@@ -27,7 +27,20 @@ export const myVotes = query({
|
|
|
27
27
|
});
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
TanStack Start server functions and server routes (behind `hub.serverFn` / `hub.request`, see [server-functions.md](server-functions.md)): check `context.hub.role`, and keep `requireFacilitator(ctx)` in any Convex function `context.convex` calls:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
export const resetEvent = createServerFn({ method: "POST" })
|
|
34
|
+
.middleware([hub.serverFn])
|
|
35
|
+
.handler(async ({ context }) => {
|
|
36
|
+
if (context.hub.role !== "facilitator") {
|
|
37
|
+
throw new Error("This is for facilitators.");
|
|
38
|
+
}
|
|
39
|
+
// …
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Any other server: check `(await verifyHubToken(request, { app })).role`.
|
|
31
44
|
|
|
32
45
|
## UI: only for a clean screen
|
|
33
46
|
|
|
@@ -40,6 +40,8 @@ export const saveSummary = createServerFn({ method: "POST" })
|
|
|
40
40
|
});
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
+
The token comes from `ConvexHubAuthProvider` (or `HubAuthProvider`), so the app must mount one, as every connected app does ([connect-an-app.md](connect-an-app.md)). Without it, a call from the browser waits 10 seconds for a provider and then fails with "Scan your badge to sign in.".
|
|
44
|
+
|
|
43
45
|
Call it like any server function (`await saveSummary({ data })`), from anywhere inside the provider: event handlers, effects, route loaders and queries (`useSuspenseQuery` included), on the first page load too. The browser half sends the person's current token, refreshed first when it is about to expire (a call made before the provider is ready, during the page's first render, waits for it): no wrapper hook, no headers at the call site. While the server renders a page (a loader or query run there) there is no browser to send a token, so it uses the person's cookie, as `getHubToken` does. Without a valid token the handler never runs and the call throws "Scan your badge to sign in.".
|
|
44
46
|
|
|
45
47
|
## 3. Server routes
|
|
@@ -73,3 +75,15 @@ Without a valid token it answers 401 "Scan your badge to sign in.".
|
|
|
73
75
|
- **Server routes take only the header**, never the cookie: a server route is an API anyone can call. Server functions take the cookie only while the server renders a page; a call from the browser always needs the token the middleware sends.
|
|
74
76
|
- **Types:** `HubConvexContext` (`context.hub`, `context.hubToken`, `context.convex`) from `@user-hub/auth/start-convex`, `HubAuthContext` from `@user-hub/auth/start-middleware`, for helpers that take the handler's context.
|
|
75
77
|
- **Create the middleware once** and import it; do not call `hubConvexMiddleware` per function.
|
|
78
|
+
|
|
79
|
+
## Moving from a hand-rolled version
|
|
80
|
+
|
|
81
|
+
Apps written before `@user-hub/auth` 0.7 may check tokens themselves. Signs: a hook that adds `Authorization: Bearer` to every server-function call (often `useHubServerFn`, wrapping `useHubToken`), and a `createMiddleware().server(...)` that reads the header, calls `verifyHubToken` and builds a `ConvexHttpClient` with `setAuth`. Replace them:
|
|
82
|
+
|
|
83
|
+
1. `bun add @user-hub/auth@latest` (0.7.1 or newer: it also covers server functions called during the first page load).
|
|
84
|
+
2. Create `hub` once, as in step 1 above, and delete the hand-rolled middleware.
|
|
85
|
+
3. Server functions: `.middleware([hub.serverFn])`. Delete the wrapper hook and call the functions directly at every call site.
|
|
86
|
+
4. Server routes: `middleware: [hub.request]`. Their callers keep sending `Authorization: Bearer <token>` from `useHubToken()`; a missing or bad token is now a 401, so check the UI shows it sensibly.
|
|
87
|
+
5. Types: use `HubConvexContext` (or `HubAuthContext`) instead of your own context type.
|
|
88
|
+
6. Keep `getHubToken` in the root route for `initialToken`, and every `requireHubUser` / `requireFacilitator` in Convex.
|
|
89
|
+
7. Run the app's checks, then test every changed path signed in, including a hard reload straight onto a page that calls a server function as it loads.
|