create-user-hub 0.4.0 → 0.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-user-hub",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
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 {
@@ -21,9 +23,26 @@ const CONVEX_URL = import.meta.env.VITE_CONVEX_URL;
21
23
  if (!CONVEX_URL) {
22
24
  throw new Error("Missing VITE_CONVEX_URL: run `bun run setup` first");
23
25
  }
24
- const convex = new ConvexReactClient(CONVEX_URL);
26
+ // expectAuth: queries wait for the person's token, so none runs signed out.
27
+ // initialAuthTokenReuse: Convex keeps that token until its refresh is due,
28
+ // instead of fetching another at once (a second sign-in that reruns every
29
+ // query).
30
+ const convex = new ConvexReactClient(CONVEX_URL, {
31
+ expectAuth: true,
32
+ initialAuthTokenReuse: true,
33
+ });
34
+
35
+ // The signed-in person's token from the request, so the server renders them
36
+ // instead of the loading state.
37
+ const fetchHubToken = createServerFn({ method: "GET" }).handler(() =>
38
+ getHubToken({ app: APP_KEY, hubUrl: import.meta.env.VITE_HUB_URL }),
39
+ );
25
40
 
26
41
  export const Route = createRootRoute({
42
+ // On the server only: in the browser the provider already has the token.
43
+ beforeLoad: async () => ({
44
+ hubToken: import.meta.env.SSR ? await fetchHubToken() : null,
45
+ }),
27
46
  head: () => ({
28
47
  meta: [
29
48
  { charSet: "utf-8" },
@@ -38,6 +57,7 @@ export const Route = createRootRoute({
38
57
  // The hub providers go inside <body>, never around <html>: they always render
39
58
  // their children, and HubSignedIn decides what a visitor sees.
40
59
  function RootDocument({ children }: { children: React.ReactNode }) {
60
+ const { hubToken } = Route.useRouteContext();
41
61
  return (
42
62
  <html lang="en" className="dark">
43
63
  <head>
@@ -48,6 +68,7 @@ function RootDocument({ children }: { children: React.ReactNode }) {
48
68
  client={convex}
49
69
  app={APP_KEY}
50
70
  hubUrl={import.meta.env.VITE_HUB_URL}
71
+ initialToken={hubToken}
51
72
  >
52
73
  <HubSignedIn signedOut={<SignedOut />} loading={<Loading />}>
53
74
  {children}
@@ -23,6 +23,11 @@ export default defineConfig({
23
23
  // that URL, so stop with an error when the port is taken instead of moving
24
24
  // to the next free one.
25
25
  server: { strictPort: true },
26
+ // One convex/react in the browser: the hub provider gives the Convex
27
+ // client to the same context the app's Convex hooks read. Pre-bundled
28
+ // together, so a re-optimization can never split them into two copies
29
+ // ("Could not find Convex client").
30
+ optimizeDeps: { include: ["convex/react", "@user-hub/auth/convex-react"] },
26
31
  plugins: lazyPlugins(() => [
27
32
  tailwindcss(),
28
33
  tanstackStart(),