@user-hub/auth 0.7.1 → 0.7.2
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 +5 -0
- package/package.json +1 -1
- package/skills/user-hub-auth/SKILL.md +2 -1
- package/skills/user-hub-auth/references/connect-an-app.md +1 -1
- package/skills/user-hub-auth/references/dev-and-production.md +4 -1
- 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
|
@@ -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
|
|
|
@@ -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,6 +16,7 @@ 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
|
|
|
@@ -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`.
|
|
@@ -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
|
|
|
@@ -16,7 +16,9 @@ Badge scans reach the app only once the hub knows its production URL. Ask the hu
|
|
|
16
16
|
- **Starter apps:** `bun run setup --prod https://<deployed url>`. It also sets `HUB_ISSUER` and `HUB_JWKS` on the production Convex deployment.
|
|
17
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
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.
|