@user-hub/auth 0.4.2 → 0.6.0
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 +43 -30
- package/dist/convex-55v4qaqb.js +123 -0
- package/dist/{convex-bymky2mf.js → convex-kpwbsxky.js} +64 -136
- package/dist/convex-react.d.ts +2 -2
- package/dist/convex-react.js +4 -2
- package/dist/convex-tx5kfb4e.js +56 -0
- package/dist/convex.d.ts +3 -3
- package/dist/convex.js +1 -1
- package/dist/internal/clock.d.ts +3 -0
- package/dist/internal/cookie.d.ts +3 -0
- package/dist/internal/refresh.d.ts +11 -0
- package/dist/internal/server-session.d.ts +19 -0
- package/dist/{session.d.ts → internal/session.d.ts} +2 -2
- package/dist/internal/storage.d.ts +3 -0
- package/dist/internal/store.d.ts +13 -0
- package/dist/{token.d.ts → internal/token.d.ts} +1 -0
- package/dist/internal/url-token.d.ts +2 -0
- package/dist/react.d.ts +4 -4
- package/dist/react.js +2 -1
- package/dist/server.d.ts +2 -2
- package/dist/server.js +3 -51
- package/dist/start.d.ts +13 -0
- package/dist/start.js +83 -0
- package/package.json +14 -7
- package/skills/user-hub-auth/SKILL.md +25 -202
- package/skills/user-hub-auth/references/connect-an-app.md +87 -0
- package/skills/user-hub-auth/references/dev-and-production.md +30 -0
- package/skills/user-hub-auth/references/roles.md +39 -0
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -521
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const MIN_LIFETIME_MS: number;
|
|
2
|
+
export type TokenCookie = {
|
|
3
|
+
get(): string | undefined;
|
|
4
|
+
set(token: string): void;
|
|
5
|
+
clear(): void;
|
|
6
|
+
};
|
|
7
|
+
export type HubTokenOptions = {
|
|
8
|
+
app: string;
|
|
9
|
+
hubUrl?: string;
|
|
10
|
+
issuer?: string;
|
|
11
|
+
jwks?: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* The cookie's token when it is genuine and has minutes left, refreshed with
|
|
15
|
+
* the hub first when it has not; null when there is nothing to render the
|
|
16
|
+
* person from. Null never means signed out: the browser still decides that,
|
|
17
|
+
* so the cookie is only cleared when the hub has ended the session.
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolveHubToken(cookie: TokenCookie, { app, hubUrl, issuer, jwks, }: HubTokenOptions): Promise<string | null>;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { type ReactNode } from "react";
|
|
2
2
|
import { type HubSession } from "./token";
|
|
3
|
-
export declare function resetHubClock(): void;
|
|
4
3
|
type HubState = {
|
|
5
4
|
session: HubSession | null | undefined;
|
|
6
5
|
redirecting: boolean;
|
|
@@ -13,6 +12,7 @@ export declare const useHubState: () => HubState;
|
|
|
13
12
|
export type HubAuthProviderProps = {
|
|
14
13
|
app: string;
|
|
15
14
|
hubUrl?: string;
|
|
15
|
+
initialToken?: string | null;
|
|
16
16
|
children: ReactNode;
|
|
17
17
|
};
|
|
18
18
|
/**
|
|
@@ -21,5 +21,5 @@ export type HubAuthProviderProps = {
|
|
|
21
21
|
* schedule (and whenever Convex asks), follows other tabs of the same app,
|
|
22
22
|
* and signs out when the hub ends the session.
|
|
23
23
|
*/
|
|
24
|
-
export declare function HubSessionProvider({ app, hubUrl, children, }: HubAuthProviderProps): import("react").JSX.Element;
|
|
24
|
+
export declare function HubSessionProvider({ app, hubUrl, initialToken, children, }: HubAuthProviderProps): import("react").JSX.Element;
|
|
25
25
|
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type HubSession } from "./token";
|
|
2
|
+
export type SessionState = {
|
|
3
|
+
session: HubSession | null | undefined;
|
|
4
|
+
stalled: string | null;
|
|
5
|
+
};
|
|
6
|
+
export declare const LOADING: SessionState;
|
|
7
|
+
export declare function serverState(app: string, token?: string | null): SessionState;
|
|
8
|
+
export declare function firstState(app: string, key: string, initialToken?: string | null): SessionState;
|
|
9
|
+
export declare function createSessionStore(first: SessionState): {
|
|
10
|
+
get: () => SessionState;
|
|
11
|
+
set: (next: Partial<SessionState>) => void;
|
|
12
|
+
subscribe: (listener: () => void) => () => void;
|
|
13
|
+
};
|
|
@@ -15,6 +15,7 @@ export type HubSession = {
|
|
|
15
15
|
user: HubClaims;
|
|
16
16
|
};
|
|
17
17
|
export declare function sessionFor(token: string | null, app: string, now?: number): HubSession | null;
|
|
18
|
+
export declare function newestSession(...sessions: (HubSession | null)[]): HubSession | null;
|
|
18
19
|
export declare function refreshInfo(token: string): {
|
|
19
20
|
url: string;
|
|
20
21
|
issuedAt: number;
|
package/dist/react.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { type ReactNode } from "react";
|
|
2
|
-
import { type HubAuthProviderProps } from "./session";
|
|
3
|
-
import type { HubSession, HubUser } from "./token";
|
|
4
|
-
export type { HubAuthProviderProps } from "./session";
|
|
5
|
-
export type { HubRole, HubUser } from "./token";
|
|
2
|
+
import { type HubAuthProviderProps } from "./internal/session";
|
|
3
|
+
import type { HubSession, HubUser } from "./internal/token";
|
|
4
|
+
export type { HubAuthProviderProps } from "./internal/session";
|
|
5
|
+
export type { HubRole, HubUser } from "./internal/token";
|
|
6
6
|
/**
|
|
7
7
|
* Provides the signed-in person to the app. Always renders its children:
|
|
8
8
|
* put it inside the root document's <body>, and use <HubSignedIn> to decide
|
package/dist/react.js
CHANGED
package/dist/server.d.ts
CHANGED
package/dist/server.js
CHANGED
|
@@ -1,54 +1,6 @@
|
|
|
1
|
-
// src/server.ts
|
|
2
1
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
jwtVerify
|
|
6
|
-
} from "jose";
|
|
7
|
-
var keySets = new Map;
|
|
8
|
-
function keySet(jwks, issuer) {
|
|
9
|
-
const source = jwks ?? `${issuer}/.well-known/jwks.json`;
|
|
10
|
-
let keys = keySets.get(source);
|
|
11
|
-
if (!keys) {
|
|
12
|
-
if (source.startsWith("data:")) {
|
|
13
|
-
const json = Buffer.from(source.split(",")[1] ?? "", "base64").toString();
|
|
14
|
-
keys = createLocalJWKSet(JSON.parse(json));
|
|
15
|
-
} else {
|
|
16
|
-
keys = createRemoteJWKSet(new URL(source));
|
|
17
|
-
}
|
|
18
|
-
keySets.set(source, keys);
|
|
19
|
-
}
|
|
20
|
-
return keys;
|
|
21
|
-
}
|
|
22
|
-
function bearer(input) {
|
|
23
|
-
if (typeof input === "string")
|
|
24
|
-
return input;
|
|
25
|
-
const header = input.headers.get("authorization") ?? "";
|
|
26
|
-
return header.startsWith("Bearer ") ? header.slice("Bearer ".length) : "";
|
|
27
|
-
}
|
|
28
|
-
async function verifyHubToken(input, {
|
|
29
|
-
app,
|
|
30
|
-
issuer = process.env.HUB_ISSUER,
|
|
31
|
-
jwks = process.env.HUB_JWKS
|
|
32
|
-
}) {
|
|
33
|
-
if (!issuer) {
|
|
34
|
-
throw new Error("Set HUB_ISSUER: run `bunx hub-user register`");
|
|
35
|
-
}
|
|
36
|
-
const token = bearer(input);
|
|
37
|
-
if (!token) {
|
|
38
|
-
throw new Error("No hub token");
|
|
39
|
-
}
|
|
40
|
-
const { payload } = await jwtVerify(token, keySet(jwks, issuer), {
|
|
41
|
-
issuer,
|
|
42
|
-
audience: app,
|
|
43
|
-
algorithms: ["ES256"]
|
|
44
|
-
});
|
|
45
|
-
return {
|
|
46
|
-
id: payload.sub,
|
|
47
|
-
name: payload.name,
|
|
48
|
-
team: payload.team,
|
|
49
|
-
role: payload.role === "facilitator" ? "facilitator" : "member"
|
|
50
|
-
};
|
|
51
|
-
}
|
|
2
|
+
verifyHubToken2
|
|
3
|
+
} from "./convex-tx5kfb4e.js";
|
|
52
4
|
export {
|
|
53
|
-
verifyHubToken
|
|
5
|
+
verifyHubToken2 as verifyHubToken
|
|
54
6
|
};
|
package/dist/start.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type HubTokenOptions } from "./internal/server-session";
|
|
2
|
+
export type { HubTokenOptions } from "./internal/server-session";
|
|
3
|
+
/**
|
|
4
|
+
* For TanStack Start: the signed-in person's token from this request's
|
|
5
|
+
* cookie, so the server renders the page signed in instead of the loading
|
|
6
|
+
* state. Call it inside a server function (it reads the request) and pass the
|
|
7
|
+
* result to the provider's `initialToken`. Checks the token with the hub's
|
|
8
|
+
* key, and refreshes it with the hub when it is about to expire. Returns null
|
|
9
|
+
* when it cannot vouch for one; the page then renders as loading and the
|
|
10
|
+
* browser settles it, as without it. A response it returns a token for is
|
|
11
|
+
* marked private and never stored, since the page then carries the token.
|
|
12
|
+
*/
|
|
13
|
+
export declare function getHubToken(options: HubTokenOptions): Promise<string | null>;
|
package/dist/start.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hubNow,
|
|
3
|
+
readClaims,
|
|
4
|
+
exchange,
|
|
5
|
+
cookieName,
|
|
6
|
+
COOKIE_MAX_AGE_S
|
|
7
|
+
} from "./convex-55v4qaqb.js";
|
|
8
|
+
import {
|
|
9
|
+
verifyHubToken2
|
|
10
|
+
} from "./convex-tx5kfb4e.js";
|
|
11
|
+
|
|
12
|
+
// src/start.ts
|
|
13
|
+
import {
|
|
14
|
+
appendResponseHeader,
|
|
15
|
+
deleteCookie,
|
|
16
|
+
getCookie,
|
|
17
|
+
getRequestProtocol,
|
|
18
|
+
setCookie,
|
|
19
|
+
setResponseHeader
|
|
20
|
+
} from "@tanstack/react-start/server";
|
|
21
|
+
|
|
22
|
+
// src/internal/server-session.ts
|
|
23
|
+
var MIN_LIFETIME_MS = 2 * 60 * 1000;
|
|
24
|
+
async function verified(token, app, issuer, jwks) {
|
|
25
|
+
try {
|
|
26
|
+
await verifyHubToken2(token, { app, issuer, jwks });
|
|
27
|
+
return true;
|
|
28
|
+
} catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function resolveHubToken(cookie, {
|
|
33
|
+
app,
|
|
34
|
+
hubUrl,
|
|
35
|
+
issuer = process.env.HUB_ISSUER || hubUrl?.replace(/\/+$/, ""),
|
|
36
|
+
jwks = process.env.HUB_JWKS || undefined
|
|
37
|
+
}) {
|
|
38
|
+
const token = cookie.get();
|
|
39
|
+
const user = token ? readClaims(token) : null;
|
|
40
|
+
if (!token || !issuer || user?.app !== app)
|
|
41
|
+
return null;
|
|
42
|
+
if (user.expiresAt - hubNow() > MIN_LIFETIME_MS) {
|
|
43
|
+
return await verified(token, app, issuer, jwks) ? token : null;
|
|
44
|
+
}
|
|
45
|
+
if (!hubUrl)
|
|
46
|
+
return null;
|
|
47
|
+
const result = await exchange(token, app, hubUrl);
|
|
48
|
+
if (result.status === "ended") {
|
|
49
|
+
cookie.clear();
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
if (result.status === "failed")
|
|
53
|
+
return null;
|
|
54
|
+
const next = result.session.token;
|
|
55
|
+
if (!await verified(next, app, issuer, jwks))
|
|
56
|
+
return null;
|
|
57
|
+
cookie.set(next);
|
|
58
|
+
return next;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/start.ts
|
|
62
|
+
async function getHubToken(options) {
|
|
63
|
+
const name = cookieName(options.app);
|
|
64
|
+
const secure = getRequestProtocol({ xForwardedProto: true }) === "https";
|
|
65
|
+
const token = await resolveHubToken({
|
|
66
|
+
get: () => getCookie(name),
|
|
67
|
+
set: (token) => setCookie(name, token, {
|
|
68
|
+
path: "/",
|
|
69
|
+
maxAge: COOKIE_MAX_AGE_S,
|
|
70
|
+
sameSite: "lax",
|
|
71
|
+
secure
|
|
72
|
+
}),
|
|
73
|
+
clear: () => deleteCookie(name, { path: "/" })
|
|
74
|
+
}, options);
|
|
75
|
+
if (token) {
|
|
76
|
+
setResponseHeader("Cache-Control", "private, no-store");
|
|
77
|
+
appendResponseHeader("Vary", "Cookie");
|
|
78
|
+
}
|
|
79
|
+
return token;
|
|
80
|
+
}
|
|
81
|
+
export {
|
|
82
|
+
getHubToken
|
|
83
|
+
};
|
package/package.json
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@user-hub/auth",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Sign people in from User Hub badges: Convex auth config, React provider
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Sign people in from User Hub badges: Convex auth config, React provider and server verification.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"auth",
|
|
7
7
|
"badge",
|
|
8
8
|
"convex",
|
|
9
9
|
"qr",
|
|
10
10
|
"react",
|
|
11
|
+
"tanstack-start",
|
|
11
12
|
"user-hub"
|
|
12
13
|
],
|
|
13
|
-
"bin": {
|
|
14
|
-
"hub-user": "./dist/cli.js"
|
|
15
|
-
},
|
|
16
14
|
"files": [
|
|
17
15
|
"dist",
|
|
18
16
|
"skills"
|
|
@@ -34,13 +32,17 @@
|
|
|
34
32
|
"./convex-react": {
|
|
35
33
|
"types": "./dist/convex-react.d.ts",
|
|
36
34
|
"default": "./dist/convex-react.js"
|
|
35
|
+
},
|
|
36
|
+
"./start": {
|
|
37
|
+
"types": "./dist/start.d.ts",
|
|
38
|
+
"default": "./dist/start.js"
|
|
37
39
|
}
|
|
38
40
|
},
|
|
39
41
|
"publishConfig": {
|
|
40
42
|
"access": "public"
|
|
41
43
|
},
|
|
42
44
|
"scripts": {
|
|
43
|
-
"build": "rm -rf dist && bun build src/convex.ts src/react.tsx src/convex-react.tsx src/server.ts src/
|
|
45
|
+
"build": "rm -rf dist && bun build src/convex.ts src/react.tsx src/convex-react.tsx src/server.ts src/start.ts --outdir dist --target node --format esm --splitting --packages external && tsc -p tsconfig.build.json",
|
|
44
46
|
"check": "vp check",
|
|
45
47
|
"fix": "vp check --fix",
|
|
46
48
|
"lint": "vp lint",
|
|
@@ -49,10 +51,11 @@
|
|
|
49
51
|
"prepublishOnly": "bun run build && bun test"
|
|
50
52
|
},
|
|
51
53
|
"dependencies": {
|
|
52
|
-
"jose": "^6.2.
|
|
54
|
+
"jose": "^6.2.18"
|
|
53
55
|
},
|
|
54
56
|
"devDependencies": {
|
|
55
57
|
"@happy-dom/global-registrator": "^21.0.4",
|
|
58
|
+
"@tanstack/react-start": "^1.169.5",
|
|
56
59
|
"@types/bun": "^1.4.4",
|
|
57
60
|
"@types/react": "^19.3.0",
|
|
58
61
|
"@types/react-dom": "^19.3.0",
|
|
@@ -63,6 +66,7 @@
|
|
|
63
66
|
"vite-plus": "1.7.0"
|
|
64
67
|
},
|
|
65
68
|
"peerDependencies": {
|
|
69
|
+
"@tanstack/react-start": ">=1.169.0",
|
|
66
70
|
"convex": ">=1.25.0",
|
|
67
71
|
"react": ">=19"
|
|
68
72
|
},
|
|
@@ -72,6 +76,9 @@
|
|
|
72
76
|
},
|
|
73
77
|
"react": {
|
|
74
78
|
"optional": true
|
|
79
|
+
},
|
|
80
|
+
"@tanstack/react-start": {
|
|
81
|
+
"optional": true
|
|
75
82
|
}
|
|
76
83
|
}
|
|
77
84
|
}
|
|
@@ -1,219 +1,42 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: user-hub-auth
|
|
3
|
-
description: Sign people into an app from their User Hub badge. Use when an app
|
|
3
|
+
description: Sign people into an app from their printed User Hub badge with @user-hub/auth. Use when connecting an app to a User Hub, adding badge or QR sign-in, wiring hubAuth, ConvexHubAuthProvider, HubAuthProvider, HubSignedIn, useHubUser or verifyHubToken, building facilitator-only pages, or moving an app to a new hub URL or to production. Use it even when the user only says "connect this to the hub" or "add sign-in for the workshop". 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. One hub runs many workshops, called **events**;
|
|
8
|
+
People at a workshop scan a printed QR badge. One hub runs many workshops, called **events**; every person and every app belongs to one. The scan sends the person, signed in, to the app that is live in their event (or to the event's app list), with a token in the URL fragment (`#token=…`) that only that app accepts. `@user-hub/auth` turns that token into a signed-in person: `{ id, name, team, role }`. The app keeps its own data, keyed on the person's hub id.
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
Registering apps, signing a folder in to the hub and managing people is done with the `user-hub` CLI: follow the **user-hub-cli** skill for any `user-hub …` command.
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
## Pick the path
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
| Situation | Do this |
|
|
15
|
+
|---|---|
|
|
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
|
+
| 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
|
+
| Facilitator-only pages or actions | [references/roles.md](references/roles.md) |
|
|
19
|
+
| Running it locally, going to production, or the hub moved | [references/dev-and-production.md](references/dev-and-production.md) |
|
|
17
20
|
|
|
18
|
-
|
|
21
|
+
A starter app is one whose `package.json` has `"setup": "user-hub setup"`: for it, prefer `bun run setup` (with `--event`, `--prod` or `--dev`) over doing the steps by hand.
|
|
19
22
|
|
|
20
|
-
|
|
23
|
+
## Ask the human first
|
|
21
24
|
|
|
22
|
-
|
|
23
|
-
cd my-app
|
|
24
|
-
bun run setup # Convex project, hub registration, HUB_ISSUER and HUB_JWKS, once
|
|
25
|
-
bun run dev # http://localhost:3000
|
|
26
|
-
```
|
|
25
|
+
Never guess these; ask:
|
|
27
26
|
|
|
28
|
-
|
|
27
|
+
1. **Which event** the app is for (the CLI lists them). Create an event only when the human asks.
|
|
28
|
+
2. **The app's name** in the hub, unique within the event.
|
|
29
|
+
3. **Its local URL** (usually `http://localhost:3000`) and its **production URL** once deployed.
|
|
30
|
+
4. **Which Convex deployments** it uses (dev, prod), so the values go on each.
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
## The hub's URL
|
|
33
|
-
|
|
34
|
-
The hub has one address, for example `https://hub.example.com`. The human gives it to you; you need it in three places, all with the same value:
|
|
35
|
-
|
|
36
|
-
- `HUB_URL` in the app's `.env.local`: read by `hub-user register` (**Register the app**). `hub-user login` puts it there, next to the API key (**Signing in**).
|
|
37
|
-
- `HUB_ISSUER` on the app's Convex deployment: `hub-user register` prints it for you (**Set the two values**).
|
|
38
|
-
- `VITE_HUB_URL` in the app's frontend env, for production builds too: lets the dev server send you to the hub to sign in, and limits token refresh to that hub (**Wire it up**).
|
|
39
|
-
|
|
40
|
-
## Before you start: ask the human
|
|
41
|
-
|
|
42
|
-
You need five things. Ask for them; never guess.
|
|
43
|
-
|
|
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.
|
|
49
|
-
|
|
50
|
-
## Signing in
|
|
51
|
-
|
|
52
|
-
`hub-user` acts as a hub account through an API key, `HUB_API_KEY`, saved in the app folder's `.env.local` next to `HUB_URL`. You never ask for an API key or password in chat, never write one into a file yourself, never commit one and never print `.env.local`. Always run the CLI as `bunx -p @user-hub/auth hub-user …`: it uses the app's installed `@user-hub/auth`, or fetches that package when the app does not have it yet. Never run a bare `bunx hub-user`: in a folder without the package it fetches an npm package called `hub-user`, which is not ours.
|
|
53
|
-
|
|
54
|
-
Sign the folder in one of two ways, from the app's folder:
|
|
55
|
-
|
|
56
|
-
- **The human runs** `bunx -p @user-hub/auth hub-user login --hub https://<hub url>` in a terminal. It asks them to paste an API key from `<hub>/account` (it shows as stars) or press Enter to sign in with the browser.
|
|
57
|
-
- **You run** `bunx -p @user-hub/auth hub-user login --hub https://<hub url> --browser`. It prints a link and a code, then waits (up to 10 minutes) until the request is approved: run it in the background or with a long timeout, read the link from its output and give it to the human to open, sign in to the hub and approve. Never open or approve the link yourself. When it is approved, the command creates an API key for itself, saves it and prints `Signed in to <hub> as <username> (<role>)`.
|
|
58
|
-
|
|
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.
|
|
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
|
-
|
|
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.
|
|
64
|
-
|
|
65
|
-
## Steps
|
|
66
|
-
|
|
67
|
-
1. **Install:**
|
|
68
|
-
|
|
69
|
-
```sh
|
|
70
|
-
bun add @user-hub/auth
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
2. **Register the app** in its event from the app's repo root, once the folder is signed in (question 4 above):
|
|
74
|
-
|
|
75
|
-
```sh
|
|
76
|
-
bunx -p @user-hub/auth hub-user register --event <slug> --name "<app name>" --new --prod https://app.example.com --dev http://localhost:3000
|
|
77
|
-
```
|
|
78
|
-
|
|
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.
|
|
84
|
-
|
|
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.
|
|
86
|
-
|
|
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.
|
|
88
|
-
|
|
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 }`).
|
|
90
|
-
|
|
91
|
-
3. **Set the two values** where tokens are checked:
|
|
92
|
-
- Convex: `bunx convex env set HUB_ISSUER '…'` and `bunx convex env set HUB_JWKS '…'` (on every deployment the app uses).
|
|
93
|
-
- Other servers: their environment, same names.
|
|
94
|
-
|
|
95
|
-
4. **Wire it up** with the key from **Register the app**.
|
|
96
|
-
|
|
97
|
-
Convex, `convex/auth.config.ts`:
|
|
98
|
-
|
|
99
|
-
```ts
|
|
100
|
-
import type { AuthConfig } from "convex/server";
|
|
101
|
-
import { hubAuth } from "@user-hub/auth/convex";
|
|
102
|
-
|
|
103
|
-
export default { providers: [hubAuth({ app: "<key>" })] } satisfies AuthConfig;
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
The provider replaces `ConvexProvider` (keep the same `ConvexReactClient`). Set `VITE_HUB_URL` (the hub's URL) in the app's frontend env.
|
|
107
|
-
|
|
108
|
-
**Where it goes:** inside the root document's `<body>`, around the page content. In TanStack Start that is the root route's shell component (`src/routes/__root.tsx`); in a Vite SPA, around `<App />`; in Next.js, inside `<body>` in the root layout. **Never** in the router's `Wrap`, or anywhere outside `<html>`.
|
|
109
|
-
|
|
110
|
-
```tsx
|
|
111
|
-
import { ConvexHubAuthProvider, HubSignedIn } from "@user-hub/auth/convex-react";
|
|
112
|
-
|
|
113
|
-
<body>
|
|
114
|
-
<ConvexHubAuthProvider
|
|
115
|
-
client={convex}
|
|
116
|
-
app="<key>"
|
|
117
|
-
hubUrl={import.meta.env.VITE_HUB_URL}
|
|
118
|
-
>
|
|
119
|
-
<HubSignedIn>{children}</HubSignedIn>
|
|
120
|
-
</ConvexHubAuthProvider>
|
|
121
|
-
<Scripts />
|
|
122
|
-
</body>
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
The provider always renders its children; `<HubSignedIn>` shows them only to a signed-in person. Without a valid token it shows "Scan your badge to sign in": pass `signedOut={…}` to style that screen and `loading={…}` for the first render. Anything outside `<HubSignedIn>` (a public landing page, say) shows to everyone. Apps without Convex use `HubAuthProvider` and `HubSignedIn` from `@user-hub/auth/react`, with the same props minus `client`.
|
|
126
|
-
|
|
127
|
-
5. **Use the person.**
|
|
128
|
-
- In components: `const me = useHubUser()` gives `{ id, name, team, role }`.
|
|
129
|
-
- In Convex functions: `const me = await requireHubUser(ctx)` gives the same `{ id, name, team, role }` (see Facilitator pages).
|
|
130
|
-
- Not Convex: wrap the app in `HubAuthProvider` and `HubSignedIn` (`@user-hub/auth/react`), send `useHubToken()` as `Authorization: Bearer <token>`, and check it on the server with `await verifyHubToken(request, { app: "<key>" })` from `@user-hub/auth/server`.
|
|
131
|
-
|
|
132
|
-
## Your dev server
|
|
133
|
-
|
|
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.
|
|
135
|
-
|
|
136
|
-
- To switch person: open `<hub>/open/<key>` and pick someone else.
|
|
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.
|
|
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".
|
|
139
|
-
|
|
140
|
-
## Production URL
|
|
141
|
-
|
|
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:
|
|
143
|
-
|
|
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.
|
|
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 '…'`.
|
|
146
|
-
|
|
147
|
-
Then:
|
|
148
|
-
|
|
149
|
-
1. `bunx convex deploy`.
|
|
150
|
-
2. On the host, set `VITE_CONVEX_URL` (the production deployment's URL) and `VITE_HUB_URL` (the hub's 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.
|
|
152
|
-
|
|
153
|
-
## Hub moved to a new URL
|
|
154
|
-
|
|
155
|
-
When the human says the hub's URL changed, hub ids stay the same, so data keyed on the id carries over. Until the app is updated it rejects every sign-in.
|
|
156
|
-
|
|
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.
|
|
158
|
-
- **Other apps:**
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
163
|
-
|
|
164
|
-
## Sign out
|
|
165
|
-
|
|
166
|
-
Use `useHubSignOut()` from the same entry point as the provider; never clear storage or tokens yourself.
|
|
167
|
-
|
|
168
|
-
```tsx
|
|
169
|
-
const signOut = useHubSignOut();
|
|
170
|
-
<button onClick={signOut}>Sign out</button>
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
It forgets this app's token only. In production the person then sees the signed-out screen and scans their badge again; on a local dev server the hub opens to pick another person.
|
|
174
|
-
|
|
175
|
-
## Facilitator pages
|
|
176
|
-
|
|
177
|
-
People are either `"member"` or `"facilitator"`, set in the hub. To give facilitators their own page or actions:
|
|
178
|
-
|
|
179
|
-
- **Protect the data on the server, always.** In Convex functions use the helpers from `@user-hub/auth/convex`:
|
|
180
|
-
|
|
181
|
-
```ts
|
|
182
|
-
import { requireFacilitator, requireHubUser } from "@user-hub/auth/convex";
|
|
183
|
-
|
|
184
|
-
export const closeVoting = mutation({
|
|
185
|
-
args: {},
|
|
186
|
-
handler: async (ctx) => {
|
|
187
|
-
const facilitator = await requireFacilitator(ctx); // throws for members
|
|
188
|
-
// …
|
|
189
|
-
},
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
export const myVotes = query({
|
|
193
|
-
args: {},
|
|
194
|
-
handler: async (ctx) => {
|
|
195
|
-
const me = await requireHubUser(ctx); // throws when signed out
|
|
196
|
-
// query by me.id
|
|
197
|
-
},
|
|
198
|
-
});
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
`getHubUser(ctx)` returns the person or null when signing in is optional. Not Convex: check `(await verifyHubToken(request, { app })).role`.
|
|
202
|
-
|
|
203
|
-
- **Then hide what members cannot use**, for a clean UI only:
|
|
204
|
-
|
|
205
|
-
```tsx
|
|
206
|
-
const me = useHubUser();
|
|
207
|
-
if (me?.role !== "facilitator") return <p>This page is for facilitators.</p>;
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
A hidden page is not protection: members can still call the functions, which is why the server check comes first.
|
|
32
|
+
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.
|
|
211
33
|
|
|
212
34
|
## Rules
|
|
213
35
|
|
|
214
|
-
- Key
|
|
215
|
-
- Check access on the server
|
|
216
|
-
-
|
|
217
|
-
-
|
|
218
|
-
-
|
|
219
|
-
-
|
|
36
|
+
- **Key data on the hub id** (`useHubUser().id`, `identity.subject` in Convex). Never on the name, the badge, or Convex's `tokenIdentifier`, which contains the hub's URL and changes if the hub moves.
|
|
37
|
+
- **Check access on the server**: `requireHubUser(ctx)` / `requireFacilitator(ctx)` in Convex, `verifyHubToken` elsewhere. `useHubUser()` is for display; hiding a page protects nothing.
|
|
38
|
+
- **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.
|
|
39
|
+
- **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.
|
|
40
|
+
- **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.
|
|
41
|
+
- **No other sign-in.** No login screens, passwords or sessions. If something is missing, it belongs in `@user-hub/auth`.
|
|
42
|
+
- **Secrets stay out of chat and files you write.** The API key lives only in `.env.local`, saved by `user-hub login`.
|