create-user-hub 0.4.0 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-user-hub",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Start a TanStack Start + Convex app with User Hub sign-in: bun create user-hub@latest my-app",
5
5
  "type": "module",
6
6
  "bin": {
@@ -7,7 +7,7 @@ A TanStack Start + Convex app whose sign-in comes from User Hub: people scan the
7
7
  - `convex/hub.ts`: this app's key in the hub (`APP_KEY`, made from its event's slug and its name), written by `bun run setup`.
8
8
  - The app belongs to one hub **event** (a workshop): its slug is `event` in `user-hub.json` (with the app's name and URLs in the hub), saved by `bun run setup`. Commit it; it holds nothing secret.
9
9
  - `convex/auth.config.ts`: `hubAuth({ app: APP_KEY })`; Convex trusts hub tokens through `HUB_ISSUER` and `HUB_JWKS` on the deployment.
10
- - `src/routes/__root.tsx`: `ConvexHubAuthProvider` and `HubSignedIn` inside `<body>`. Never move them outside `<html>` or into the router's `Wrap`.
10
+ - `src/routes/__root.tsx`: `ConvexHubAuthProvider` and `HubSignedIn` inside `<body>`. Never move them outside `<html>` or into the router's `Wrap`. Its `beforeLoad` reads the person's token on the server (`getHubToken`) and passes it as `initialToken`, so pages render signed in from the server: keep page components safe to render there (no `window` or `document` outside effects).
11
11
  - People: `useHubUser()` in components; `requireHubUser(ctx)` / `requireFacilitator(ctx)` in Convex functions. Key data on the person's hub id; check roles on the server.
12
12
  - Sign out: `useHubSignOut()`.
13
13
 
@@ -31,7 +31,7 @@ Sign this folder in to the new hub (`bunx user-hub login --hub <new url>`, which
31
31
 
32
32
  ## What's inside
33
33
 
34
- - `src/routes/__root.tsx`: the hub providers inside `<body>`; `HubSignedIn` shows pages only to signed-in people.
34
+ - `src/routes/__root.tsx`: the hub providers inside `<body>`; `HubSignedIn` shows pages only to signed-in people. The server reads the person's token from a cookie, so pages render signed in from the first paint.
35
35
  - `src/routes/index.tsx`: your name, team and role, what the server sees, sign-out, and notes saved per person.
36
36
  - `src/routes/facilitator.tsx`: a facilitator-only page.
37
37
  - `convex/`: notes keyed on the person's hub id. Public functions check who is asking (`requireHubUser`, `requireFacilitator`) and call `convex/model/`.
@@ -19,7 +19,7 @@
19
19
  "@tailwindcss/vite": "^4.3.5",
20
20
  "@tanstack/react-router": "^1.170.46",
21
21
  "@tanstack/react-start": "^1.169.7",
22
- "@user-hub/auth": "^0.5.0",
22
+ "@user-hub/auth": "^0.6.0",
23
23
  "class-variance-authority": "^0.7.1",
24
24
  "cn": "^0.5.2",
25
25
  "convex": "^1.51.0",
@@ -1,8 +1,10 @@
1
1
  import { createRootRoute, HeadContent, Scripts } from "@tanstack/react-router";
2
+ import { createServerFn } from "@tanstack/react-start";
2
3
  import {
3
4
  ConvexHubAuthProvider,
4
5
  HubSignedIn,
5
6
  } from "@user-hub/auth/convex-react";
7
+ import { getHubToken } from "@user-hub/auth/start";
6
8
  import { ConvexReactClient } from "convex/react";
7
9
 
8
10
  import {
@@ -23,7 +25,17 @@ if (!CONVEX_URL) {
23
25
  }
24
26
  const convex = new ConvexReactClient(CONVEX_URL);
25
27
 
28
+ // The signed-in person's token from the request, so the server renders them
29
+ // instead of the loading state.
30
+ const fetchHubToken = createServerFn({ method: "GET" }).handler(() =>
31
+ getHubToken({ app: APP_KEY, hubUrl: import.meta.env.VITE_HUB_URL }),
32
+ );
33
+
26
34
  export const Route = createRootRoute({
35
+ // On the server only: in the browser the provider already has the token.
36
+ beforeLoad: async () => ({
37
+ hubToken: import.meta.env.SSR ? await fetchHubToken() : null,
38
+ }),
27
39
  head: () => ({
28
40
  meta: [
29
41
  { charSet: "utf-8" },
@@ -38,6 +50,7 @@ export const Route = createRootRoute({
38
50
  // The hub providers go inside <body>, never around <html>: they always render
39
51
  // their children, and HubSignedIn decides what a visitor sees.
40
52
  function RootDocument({ children }: { children: React.ReactNode }) {
53
+ const { hubToken } = Route.useRouteContext();
41
54
  return (
42
55
  <html lang="en" className="dark">
43
56
  <head>
@@ -48,6 +61,7 @@ function RootDocument({ children }: { children: React.ReactNode }) {
48
61
  client={convex}
49
62
  app={APP_KEY}
50
63
  hubUrl={import.meta.env.VITE_HUB_URL}
64
+ initialToken={hubToken}
51
65
  >
52
66
  <HubSignedIn signedOut={<SignedOut />} loading={<Loading />}>
53
67
  {children}