@user-hub/auth 0.4.1 → 0.5.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 +10 -29
- package/dist/{convex-qxqkx9zh.js → convex-89hjtnwh.js} +119 -72
- package/dist/convex-react.d.ts +2 -2
- package/dist/convex-react.js +1 -1
- package/dist/convex.d.ts +3 -3
- package/dist/convex.js +1 -1
- package/dist/internal/clock.d.ts +3 -0
- package/dist/internal/refresh.d.ts +11 -0
- package/dist/{session.d.ts → internal/session.d.ts} +0 -1
- package/dist/internal/storage.d.ts +3 -0
- package/dist/internal/store.d.ts +13 -0
- package/dist/internal/url-token.d.ts +2 -0
- package/dist/react.d.ts +4 -4
- package/dist/react.js +1 -1
- package/dist/server.d.ts +2 -2
- package/dist/server.js +4 -1
- package/package.json +35 -35
- package/skills/user-hub-auth/SKILL.md +24 -202
- package/skills/user-hub-auth/references/connect-an-app.md +67 -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 -517
- /package/dist/{token.d.ts → internal/token.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ The hub decides how long a scan keeps someone signed in: 1, 2 or 4 hours, or unt
|
|
|
32
32
|
bun add @user-hub/auth
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
Using a coding agent? The package ships
|
|
35
|
+
Using a coding agent? The package ships the `user-hub-auth` skill (and `user-hub` ships `user-hub-cli`, for the commands). Add them to your agents with:
|
|
36
36
|
|
|
37
37
|
```sh
|
|
38
38
|
bunx skills experimental_sync
|
|
@@ -40,30 +40,16 @@ bunx skills experimental_sync
|
|
|
40
40
|
|
|
41
41
|
### 2. Sign in and register your app
|
|
42
42
|
|
|
43
|
-
Ask the hub's admin for its URL and an account
|
|
43
|
+
Register the app with the [`user-hub`](https://www.npmjs.com/package/user-hub) CLI. Ask the hub's admin for its URL and an account, then from your app's folder:
|
|
44
44
|
|
|
45
45
|
```sh
|
|
46
|
-
|
|
46
|
+
bun add -d user-hub
|
|
47
|
+
bunx user-hub login --hub https://hub.example.com
|
|
48
|
+
bunx user-hub events list
|
|
49
|
+
bunx user-hub apps register --event spring-workshop --name "Your App" --new --prod https://your-app.com --dev http://localhost:3000
|
|
47
50
|
```
|
|
48
51
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
A hub runs many **events** (workshops); people, apps and the schedule each belong to one. Find the event your app is for by its slug, or create one (its time zone defaults to your machine's; pass `--time-zone Europe/Berlin` to pick another):
|
|
52
|
-
|
|
53
|
-
```sh
|
|
54
|
-
bunx hub-user events
|
|
55
|
-
bunx hub-user events create --name "Spring Workshop"
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
Hubs with events need `@user-hub/auth` 0.4 or newer. Upgrading from 0.3? Run `bun add @user-hub/auth@latest` first (a `^0.3` range never installs 0.4); no app code changes.
|
|
59
|
-
|
|
60
|
-
Then register the app in that event (`bunx` reads `.env` and `.env.local` automatically; if `HUB_URL` and `HUB_API_KEY` live in a secret manager, run it through that):
|
|
61
|
-
|
|
62
|
-
```sh
|
|
63
|
-
bunx hub-user register --event spring-workshop --name "Your App" --new --prod https://your-app.com --dev http://localhost:3000
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
`--event` is needed on every run; set `HUB_EVENT=<slug>` in `.env.local` to leave it out. An unknown slug exits with code 5. It prints your **app key** (made from the event's slug and the name) and the values to set. Not deployed yet? Leave out `--prod`: you can develop, and badge scans start reaching the app once you add it. Run it again with the same `--event` and `--name` to update the URLs; a URL left out keeps the registered one. `--new` refuses to touch an app that already has that name, so you never take over someone else's; leave it out when updating your own app later. New apps are **listed** in the badge app list by default: when the hub shows the app list instead of one live app, listed apps with a production URL appear in it. Pass `--unlisted` to keep one out of it (it can still be scheduled or forced live), or `--listed` to show it again. Pass neither on an update and the hub's current setting stays; the output shows it. Any hub account can update any app, so register only under your app's own name. (The same can be done under **Apps** in the hub, with the values under **Setup**.) Quote a value with spaces (`--name "Summer Camp"`): an unexpected extra word stops any `hub-user` command with exit code 1 before it sends anything. When you pass `--hub`, the commands the CLI suggests next keep it.
|
|
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 the same way. `--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`.
|
|
67
53
|
|
|
68
54
|
### 3. Set the values
|
|
69
55
|
|
|
@@ -127,7 +113,7 @@ export const myVotes = query({
|
|
|
127
113
|
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).
|
|
128
114
|
|
|
129
115
|
- **Switch person:** open `<hub>/open/<your-app-key>`.
|
|
130
|
-
- **Different port:** run `hub
|
|
116
|
+
- **Different port:** run `user-hub apps register` again (same `--event` and `--name`) with the new `--dev` URL. The hub only sends tokens to registered URLs.
|
|
131
117
|
- **Production:** someone without a token sees "Scan your badge to sign in". Pass `signedOut={…}` to `<HubSignedIn>` to style that screen.
|
|
132
118
|
|
|
133
119
|
## Sign out
|
|
@@ -193,12 +179,6 @@ Apps without Convex use `HubAuthProvider` and `HubSignedIn` from `@user-hub/auth
|
|
|
193
179
|
| | `useHubUser()`, `useHubToken()`, `useHubSignOut()` | The person (or null), the current token (or null; it changes as it is refreshed), a sign-out function. Also from `/convex-react` |
|
|
194
180
|
| | `useHubSession()` | `undefined` while loading, `null` when signed out, otherwise `{ token, user }`; most apps need only `useHubUser()` or `<HubSignedIn>`. Also from `/convex-react` |
|
|
195
181
|
| `@user-hub/auth/server` | `verifyHubToken(requestOrToken, { app })` | Checks a token anywhere else |
|
|
196
|
-
| CLI | `hub-user login [--hub <url>] [--browser\|--api-key-stdin]` | Signs the folder in: an API key pasted (hidden) or from stdin, or a browser sign-in it prints a link for (it never opens a browser). Saves `HUB_URL` and `HUB_API_KEY` to `./.env.local` (mode 600) |
|
|
197
|
-
| | `hub-user whoami [--json]` | The account `HUB_API_KEY` belongs to; exit code 4 when not signed in |
|
|
198
|
-
| | `hub-user events [--json]` | The hub's events, newest first: slug, name, time zone, ended ones marked. `--json` prints the array of `{ slug, name, status, timeZone }` |
|
|
199
|
-
| | `hub-user events create --name <name> [--time-zone <zone>] [--json]` | Creates an event (the IANA time zone defaults to this machine's); exit code 3 if its slug is taken. `--json` prints the created event |
|
|
200
|
-
| | `hub-user register --event <slug> --name <name> [--prod <url>] [--dev <url>] [--listed\|--unlisted] [--new] [--hub <url>] [--json]` | Registers or updates the app in an event (`--event` falls back to `HUB_EVENT`; exit code 5 when no event has that slug; a new app needs at least one URL; an update may leave both out); `--new` only creates, exit code 3 if the name is taken (also without `--new` when the key belongs to an app of another event); `--json` includes `event: { slug, name }`; `--listed`/`--unlisted` set whether the app shows in the badge app list once it has a production URL (new apps default to listed, omitted keeps the hub's setting) |
|
|
201
|
-
| | Exit codes | 0 done, 1 error (an unexpected extra word, such as an unquoted `--name Summer Camp`, stops before any request), 3 name taken (`register`, `events create`), 4 not signed in (run `hub-user login`), 5 unknown event (`register`) |
|
|
202
182
|
|
|
203
183
|
## Developing this package
|
|
204
184
|
|
|
@@ -206,7 +186,8 @@ Apps without Convex use `HubAuthProvider` and `HubSignedIn` from `@user-hub/auth
|
|
|
206
186
|
bun install
|
|
207
187
|
bun test # unit tests
|
|
208
188
|
bunx tsc --noEmit -p . # type-check
|
|
209
|
-
bun run check #
|
|
189
|
+
bun run check # Vite+: format (Oxfmt), lint (Oxlint), type-check
|
|
190
|
+
bun run fix # format and apply lint fixes
|
|
210
191
|
bun run build # dist/: commit it
|
|
211
192
|
```
|
|
212
193
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// src/react.tsx
|
|
2
2
|
import { useMemo as useMemo2 } from "react";
|
|
3
3
|
|
|
4
|
-
// src/session.tsx
|
|
4
|
+
// src/internal/session.tsx
|
|
5
5
|
import {
|
|
6
6
|
createContext,
|
|
7
7
|
useCallback,
|
|
@@ -9,10 +9,18 @@ import {
|
|
|
9
9
|
useEffect,
|
|
10
10
|
useMemo,
|
|
11
11
|
useRef,
|
|
12
|
-
useState
|
|
12
|
+
useState,
|
|
13
|
+
useSyncExternalStore
|
|
13
14
|
} from "react";
|
|
14
15
|
|
|
15
|
-
// src/
|
|
16
|
+
// src/internal/clock.ts
|
|
17
|
+
var clockSkewMs = 0;
|
|
18
|
+
var hubNow = () => Date.now() + clockSkewMs;
|
|
19
|
+
function learnHubClock(issuedAt) {
|
|
20
|
+
clockSkewMs = issuedAt - Date.now();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/internal/token.ts
|
|
16
24
|
function decodeSegment(segment) {
|
|
17
25
|
const base64 = segment.replace(/-/g, "+").replace(/_/g, "/");
|
|
18
26
|
return JSON.parse(atob(base64.padEnd(Math.ceil(base64.length / 4) * 4, "=")));
|
|
@@ -26,7 +34,7 @@ function payloadOf(token) {
|
|
|
26
34
|
}
|
|
27
35
|
function readClaims(token) {
|
|
28
36
|
const payload = payloadOf(token);
|
|
29
|
-
if (typeof payload?.sub !== "string" || typeof payload.exp !== "number" || typeof payload.aud !== "string") {
|
|
37
|
+
if (typeof payload?.sub !== "string" || typeof payload.exp !== "number" || typeof payload.aud !== "string" || typeof payload.auth_time !== "number") {
|
|
30
38
|
return null;
|
|
31
39
|
}
|
|
32
40
|
return {
|
|
@@ -44,7 +52,7 @@ function sessionFor(token, app, now = Date.now()) {
|
|
|
44
52
|
}
|
|
45
53
|
function refreshInfo(token) {
|
|
46
54
|
const payload = payloadOf(token);
|
|
47
|
-
if (typeof payload?.iss !== "string" || typeof payload.iat !== "number"
|
|
55
|
+
if (typeof payload?.iss !== "string" || typeof payload.iat !== "number") {
|
|
48
56
|
return null;
|
|
49
57
|
}
|
|
50
58
|
return {
|
|
@@ -54,63 +62,14 @@ function refreshInfo(token) {
|
|
|
54
62
|
}
|
|
55
63
|
function sessionKeyOf(token) {
|
|
56
64
|
const payload = payloadOf(token);
|
|
57
|
-
if (typeof payload?.sub !== "string") {
|
|
65
|
+
if (typeof payload?.sub !== "string" || typeof payload.auth_time !== "number") {
|
|
58
66
|
return null;
|
|
59
67
|
}
|
|
60
|
-
return [
|
|
61
|
-
payload.iss,
|
|
62
|
-
payload.aud,
|
|
63
|
-
payload.sub,
|
|
64
|
-
payload.auth_time ?? payload.iat
|
|
65
|
-
].join("|");
|
|
68
|
+
return [payload.iss, payload.aud, payload.sub, payload.auth_time].join("|");
|
|
66
69
|
}
|
|
67
70
|
|
|
68
|
-
// src/
|
|
69
|
-
import { jsx } from "react/jsx-runtime";
|
|
70
|
-
var MAX_TIMEOUT_MS = 2 ** 31 - 1;
|
|
71
|
-
var REFRESH_AFTER_MS = 5 * 60 * 1000;
|
|
72
|
-
var REFRESH_BEFORE_EXPIRY_MS = 60 * 1000;
|
|
73
|
-
var RETRY_MS = 30 * 1000;
|
|
71
|
+
// src/internal/refresh.ts
|
|
74
72
|
var REQUEST_TIMEOUT_MS = 10 * 1000;
|
|
75
|
-
var FRESH_MS = 5 * 1000;
|
|
76
|
-
var clockSkewMs = 0;
|
|
77
|
-
var hubNow = () => Date.now() + clockSkewMs;
|
|
78
|
-
var LOCAL_HOST = /^(localhost|127(\.\d{1,3}){3}|10(\.\d{1,3}){3}|192\.168(\.\d{1,3}){2}|172\.(1[6-9]|2\d|3[01])(\.\d{1,3}){2}|\[::1\]|[a-z0-9-]+\.local)$/i;
|
|
79
|
-
var HubContext = createContext({
|
|
80
|
-
session: undefined,
|
|
81
|
-
redirecting: false,
|
|
82
|
-
signOut: () => {},
|
|
83
|
-
suspend: () => {},
|
|
84
|
-
currentToken: () => null,
|
|
85
|
-
refresh: async () => null
|
|
86
|
-
});
|
|
87
|
-
var useHubState = () => useContext(HubContext);
|
|
88
|
-
var storageKey = (app) => `user-hub-token:${app}`;
|
|
89
|
-
function readStorage(key) {
|
|
90
|
-
try {
|
|
91
|
-
return window.localStorage.getItem(key);
|
|
92
|
-
} catch {
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
function writeStorage(key, token) {
|
|
97
|
-
try {
|
|
98
|
-
if (token)
|
|
99
|
-
window.localStorage.setItem(key, token);
|
|
100
|
-
else
|
|
101
|
-
window.localStorage.removeItem(key);
|
|
102
|
-
} catch {}
|
|
103
|
-
}
|
|
104
|
-
function takeTokenFromUrl() {
|
|
105
|
-
const hash = new URLSearchParams(window.location.hash.slice(1));
|
|
106
|
-
const token = hash.get("token");
|
|
107
|
-
if (token) {
|
|
108
|
-
hash.delete("token");
|
|
109
|
-
const rest = hash.toString();
|
|
110
|
-
window.history.replaceState(window.history.state, "", `${window.location.pathname}${window.location.search}${rest ? `#${rest}` : ""}`);
|
|
111
|
-
}
|
|
112
|
-
return token;
|
|
113
|
-
}
|
|
114
73
|
function originOf(url) {
|
|
115
74
|
try {
|
|
116
75
|
return new URL(url).origin;
|
|
@@ -120,8 +79,12 @@ function originOf(url) {
|
|
|
120
79
|
}
|
|
121
80
|
function refreshUrlFor(token, app, hubUrl) {
|
|
122
81
|
const info = readClaims(token)?.app === app ? refreshInfo(token) : null;
|
|
123
|
-
|
|
124
|
-
|
|
82
|
+
if (!info)
|
|
83
|
+
return null;
|
|
84
|
+
if (hubUrl === undefined || hubUrl === "")
|
|
85
|
+
return info.url;
|
|
86
|
+
const hub = originOf(hubUrl);
|
|
87
|
+
return hub && originOf(info.url) === hub ? info.url : null;
|
|
125
88
|
}
|
|
126
89
|
async function exchange(token, app, hubUrl) {
|
|
127
90
|
const url = refreshUrlFor(token, app, hubUrl);
|
|
@@ -145,7 +108,7 @@ async function exchange(token, app, hubUrl) {
|
|
|
145
108
|
}
|
|
146
109
|
const issuedAt = refreshInfo(next)?.issuedAt;
|
|
147
110
|
if (issuedAt) {
|
|
148
|
-
|
|
111
|
+
learnHubClock(issuedAt);
|
|
149
112
|
}
|
|
150
113
|
const session = sessionFor(next, app, hubNow());
|
|
151
114
|
return session ? { status: "refreshed", session } : { status: "failed" };
|
|
@@ -155,16 +118,102 @@ async function exchange(token, app, hubUrl) {
|
|
|
155
118
|
clearTimeout(timeout);
|
|
156
119
|
}
|
|
157
120
|
}
|
|
121
|
+
|
|
122
|
+
// src/internal/storage.ts
|
|
123
|
+
var storageKey = (app) => `user-hub-token:${app}`;
|
|
124
|
+
function readStorage(key) {
|
|
125
|
+
try {
|
|
126
|
+
return window.localStorage.getItem(key);
|
|
127
|
+
} catch {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function writeStorage(key, token) {
|
|
132
|
+
try {
|
|
133
|
+
if (token)
|
|
134
|
+
window.localStorage.setItem(key, token);
|
|
135
|
+
else
|
|
136
|
+
window.localStorage.removeItem(key);
|
|
137
|
+
} catch {}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// src/internal/url-token.ts
|
|
141
|
+
function tokenInUrl() {
|
|
142
|
+
return new URLSearchParams(window.location.hash.slice(1)).get("token");
|
|
143
|
+
}
|
|
144
|
+
function takeTokenFromUrl() {
|
|
145
|
+
const hash = new URLSearchParams(window.location.hash.slice(1));
|
|
146
|
+
const token = hash.get("token");
|
|
147
|
+
if (token) {
|
|
148
|
+
hash.delete("token");
|
|
149
|
+
const rest = hash.toString();
|
|
150
|
+
window.history.replaceState(window.history.state, "", `${window.location.pathname}${window.location.search}${rest ? `#${rest}` : ""}`);
|
|
151
|
+
}
|
|
152
|
+
return token;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/internal/store.ts
|
|
156
|
+
var LOADING = { session: undefined, stalled: null };
|
|
157
|
+
var loadingState = () => LOADING;
|
|
158
|
+
function sameSession(a, b) {
|
|
159
|
+
return a === b || a != null && b != null && a.token === b.token;
|
|
160
|
+
}
|
|
161
|
+
function firstState(app, key) {
|
|
162
|
+
if (typeof window === "undefined")
|
|
163
|
+
return LOADING;
|
|
164
|
+
const session = sessionFor(tokenInUrl(), app, hubNow()) ?? sessionFor(readStorage(key), app, hubNow());
|
|
165
|
+
return session ? { session, stalled: null } : LOADING;
|
|
166
|
+
}
|
|
167
|
+
function createSessionStore(first) {
|
|
168
|
+
let state = first;
|
|
169
|
+
const listeners = new Set;
|
|
170
|
+
return {
|
|
171
|
+
get: () => state,
|
|
172
|
+
set: (next) => {
|
|
173
|
+
const merged = { ...state, ...next };
|
|
174
|
+
if (sameSession(merged.session, state.session) && merged.stalled === state.stalled) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
state = merged;
|
|
178
|
+
for (const listener of listeners)
|
|
179
|
+
listener();
|
|
180
|
+
},
|
|
181
|
+
subscribe: (listener) => {
|
|
182
|
+
listeners.add(listener);
|
|
183
|
+
return () => {
|
|
184
|
+
listeners.delete(listener);
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// src/internal/session.tsx
|
|
191
|
+
import { jsx } from "react/jsx-runtime";
|
|
192
|
+
var MAX_TIMEOUT_MS = 2 ** 31 - 1;
|
|
193
|
+
var REFRESH_AFTER_MS = 5 * 60 * 1000;
|
|
194
|
+
var REFRESH_BEFORE_EXPIRY_MS = 60 * 1000;
|
|
195
|
+
var RETRY_MS = 30 * 1000;
|
|
196
|
+
var FRESH_MS = 5 * 1000;
|
|
197
|
+
var LOCAL_HOST = /^(localhost|127(\.\d{1,3}){3}|10(\.\d{1,3}){3}|192\.168(\.\d{1,3}){2}|172\.(1[6-9]|2\d|3[01])(\.\d{1,3}){2}|\[::1\]|[a-z0-9-]+\.local)$/i;
|
|
198
|
+
var HubContext = createContext({
|
|
199
|
+
session: undefined,
|
|
200
|
+
redirecting: false,
|
|
201
|
+
signOut: () => {},
|
|
202
|
+
suspend: () => {},
|
|
203
|
+
currentToken: () => null,
|
|
204
|
+
refresh: async () => null
|
|
205
|
+
});
|
|
206
|
+
var useHubState = () => useContext(HubContext);
|
|
158
207
|
function HubSessionProvider({
|
|
159
208
|
app,
|
|
160
209
|
hubUrl,
|
|
161
210
|
children
|
|
162
211
|
}) {
|
|
163
212
|
const key = storageKey(app);
|
|
164
|
-
const [
|
|
165
|
-
const
|
|
166
|
-
const latest = useRef(
|
|
167
|
-
const held = useRef(null);
|
|
213
|
+
const [store] = useState(() => createSessionStore(firstState(app, key)));
|
|
214
|
+
const { session, stalled } = useSyncExternalStore(store.subscribe, store.get, loadingState);
|
|
215
|
+
const latest = useRef(store.get().session);
|
|
216
|
+
const held = useRef(store.get().session?.token ?? null);
|
|
168
217
|
const inFlight = useRef(null);
|
|
169
218
|
const mounted = useRef(false);
|
|
170
219
|
const lastRequest = useRef(0);
|
|
@@ -172,9 +221,8 @@ function HubSessionProvider({
|
|
|
172
221
|
const show = useCallback((next, token, stalledToken) => {
|
|
173
222
|
held.current = token;
|
|
174
223
|
latest.current = next;
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}, []);
|
|
224
|
+
store.set({ session: next, stalled: stalledToken });
|
|
225
|
+
}, [store]);
|
|
178
226
|
const signIn = useCallback((next) => {
|
|
179
227
|
writeStorage(key, next.token);
|
|
180
228
|
show(next, next.token, null);
|
|
@@ -258,10 +306,9 @@ function HubSessionProvider({
|
|
|
258
306
|
writeStorage(key, expired);
|
|
259
307
|
held.current = expired;
|
|
260
308
|
latest.current = undefined;
|
|
261
|
-
|
|
262
|
-
setStalled(null);
|
|
309
|
+
store.set(LOADING);
|
|
263
310
|
trade(expired);
|
|
264
|
-
}, [app, hubUrl, key, signIn, end, trade]);
|
|
311
|
+
}, [app, hubUrl, key, store, signIn, end, trade]);
|
|
265
312
|
useEffect(() => {
|
|
266
313
|
if (!stalled)
|
|
267
314
|
return;
|
|
@@ -328,8 +375,8 @@ function HubSessionProvider({
|
|
|
328
375
|
const signOut = end;
|
|
329
376
|
const suspend = useCallback(() => {
|
|
330
377
|
latest.current = null;
|
|
331
|
-
|
|
332
|
-
}, []);
|
|
378
|
+
store.set({ session: null });
|
|
379
|
+
}, [store]);
|
|
333
380
|
const state = useMemo(() => ({ session, redirecting, signOut, suspend, currentToken, refresh }), [session, redirecting, signOut, suspend, currentToken, refresh]);
|
|
334
381
|
return /* @__PURE__ */ jsx(HubContext.Provider, {
|
|
335
382
|
value: state,
|
package/dist/convex-react.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type ConvexReactClient } from "convex/react";
|
|
2
|
-
import { type HubAuthProviderProps } from "./session";
|
|
2
|
+
import { type HubAuthProviderProps } from "./internal/session";
|
|
3
3
|
export { HubSignedIn, useHubSession, useHubSignOut, useHubToken, useHubUser, } from "./react";
|
|
4
|
-
export type { HubRole, HubUser } from "./token";
|
|
4
|
+
export type { HubRole, HubUser } from "./internal/token";
|
|
5
5
|
/**
|
|
6
6
|
* HubAuthProvider for apps on Convex: the hub token becomes the Convex
|
|
7
7
|
* identity. Use it in place of ConvexProvider, with the same client, inside
|
package/dist/convex-react.js
CHANGED
package/dist/convex.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Auth, AuthConfig } from "convex/server";
|
|
2
|
-
import type { HubUser } from "./token";
|
|
3
|
-
export type { HubRole, HubUser } from "./token";
|
|
2
|
+
import type { HubUser } from "./internal/token";
|
|
3
|
+
export type { HubRole, HubUser } from "./internal/token";
|
|
4
4
|
type Provider = AuthConfig["providers"][number];
|
|
5
5
|
/**
|
|
6
6
|
* The Convex auth provider for hub tokens. In `convex/auth.config.ts`:
|
|
@@ -8,7 +8,7 @@ type Provider = AuthConfig["providers"][number];
|
|
|
8
8
|
* export default { providers: [hubAuth({ app: "voting" })] } satisfies AuthConfig;
|
|
9
9
|
*
|
|
10
10
|
* Reads HUB_ISSUER and HUB_JWKS from the Convex deployment's environment
|
|
11
|
-
* (`bunx hub
|
|
11
|
+
* (`bunx user-hub apps register` prints both). Then `ctx.auth.getUserIdentity()`
|
|
12
12
|
* returns the person: `subject` is their hub id, plus `name`, `team`, `role`.
|
|
13
13
|
*/
|
|
14
14
|
export declare function hubAuth({ app }: {
|
package/dist/convex.js
CHANGED
|
@@ -4,7 +4,7 @@ function hubAuth({ app }) {
|
|
|
4
4
|
const issuer = process.env.HUB_ISSUER;
|
|
5
5
|
const jwks = process.env.HUB_JWKS;
|
|
6
6
|
if (!issuer || !jwks) {
|
|
7
|
-
throw new Error("Set HUB_ISSUER and HUB_JWKS on this Convex deployment: run `bunx hub
|
|
7
|
+
throw new Error("Set HUB_ISSUER and HUB_JWKS on this Convex deployment: run `bunx user-hub apps register`");
|
|
8
8
|
}
|
|
9
9
|
return {
|
|
10
10
|
type: "customJwt",
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type HubSession } from "./token";
|
|
2
|
+
export declare function refreshUrlFor(token: string, app: string, hubUrl?: string): string | null;
|
|
3
|
+
export type Exchange = {
|
|
4
|
+
status: "refreshed";
|
|
5
|
+
session: HubSession;
|
|
6
|
+
} | {
|
|
7
|
+
status: "ended";
|
|
8
|
+
} | {
|
|
9
|
+
status: "failed";
|
|
10
|
+
};
|
|
11
|
+
export declare function exchange(token: string, app: string, hubUrl?: string): Promise<Exchange>;
|
|
@@ -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 const loadingState: () => SessionState;
|
|
8
|
+
export declare function firstState(app: string, key: string): SessionState;
|
|
9
|
+
export declare function createSessionStore(first: SessionState): {
|
|
10
|
+
get: () => SessionState;
|
|
11
|
+
set: (next: Partial<SessionState>) => void;
|
|
12
|
+
subscribe: (listener: () => void) => () => void;
|
|
13
|
+
};
|
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
|
@@ -31,7 +31,7 @@ async function verifyHubToken(input, {
|
|
|
31
31
|
jwks = process.env.HUB_JWKS
|
|
32
32
|
}) {
|
|
33
33
|
if (!issuer) {
|
|
34
|
-
throw new Error("Set HUB_ISSUER: run `bunx hub
|
|
34
|
+
throw new Error("Set HUB_ISSUER: run `bunx user-hub apps register`");
|
|
35
35
|
}
|
|
36
36
|
const token = bearer(input);
|
|
37
37
|
if (!token) {
|
|
@@ -42,6 +42,9 @@ async function verifyHubToken(input, {
|
|
|
42
42
|
audience: app,
|
|
43
43
|
algorithms: ["ES256"]
|
|
44
44
|
});
|
|
45
|
+
if (typeof payload.auth_time !== "number") {
|
|
46
|
+
throw new Error("Not a hub sign-in token: it has no auth_time");
|
|
47
|
+
}
|
|
45
48
|
return {
|
|
46
49
|
id: payload.sub,
|
|
47
50
|
name: payload.name,
|
package/package.json
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
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
|
|
5
|
-
"
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Sign people in from User Hub badges: Convex auth config, React provider and server verification.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"auth",
|
|
7
|
+
"badge",
|
|
8
|
+
"convex",
|
|
9
|
+
"qr",
|
|
10
|
+
"react",
|
|
11
|
+
"user-hub"
|
|
12
|
+
],
|
|
6
13
|
"files": [
|
|
7
14
|
"dist",
|
|
8
15
|
"skills"
|
|
9
16
|
],
|
|
10
|
-
"
|
|
11
|
-
"hub-user": "./dist/cli.js"
|
|
12
|
-
},
|
|
17
|
+
"type": "module",
|
|
13
18
|
"exports": {
|
|
14
19
|
"./convex": {
|
|
15
20
|
"types": "./dist/convex.d.ts",
|
|
@@ -28,15 +33,35 @@
|
|
|
28
33
|
"default": "./dist/convex-react.js"
|
|
29
34
|
}
|
|
30
35
|
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
31
39
|
"scripts": {
|
|
32
|
-
"build": "rm -rf dist && bun build src/convex.ts src/react.tsx src/convex-react.tsx src/server.ts
|
|
33
|
-
"check": "
|
|
40
|
+
"build": "rm -rf dist && bun build src/convex.ts src/react.tsx src/convex-react.tsx src/server.ts --outdir dist --target node --format esm --splitting --packages external && tsc -p tsconfig.build.json",
|
|
41
|
+
"check": "vp check",
|
|
42
|
+
"fix": "vp check --fix",
|
|
43
|
+
"lint": "vp lint",
|
|
44
|
+
"fmt": "vp fmt",
|
|
34
45
|
"test": "bun test",
|
|
35
46
|
"prepublishOnly": "bun run build && bun test"
|
|
36
47
|
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"jose": "^6.2.18"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@happy-dom/global-registrator": "^21.0.4",
|
|
53
|
+
"@types/bun": "^1.4.4",
|
|
54
|
+
"@types/react": "^19.3.0",
|
|
55
|
+
"@types/react-dom": "^19.3.0",
|
|
56
|
+
"convex": "^1.51.0",
|
|
57
|
+
"react": "^19.3.1",
|
|
58
|
+
"react-dom": "^19.3.1",
|
|
59
|
+
"typescript": "^7.0.2",
|
|
60
|
+
"vite-plus": "1.7.0"
|
|
61
|
+
},
|
|
37
62
|
"peerDependencies": {
|
|
38
63
|
"convex": ">=1.25.0",
|
|
39
|
-
"react": ">=
|
|
64
|
+
"react": ">=19"
|
|
40
65
|
},
|
|
41
66
|
"peerDependenciesMeta": {
|
|
42
67
|
"convex": {
|
|
@@ -45,30 +70,5 @@
|
|
|
45
70
|
"react": {
|
|
46
71
|
"optional": true
|
|
47
72
|
}
|
|
48
|
-
}
|
|
49
|
-
"dependencies": {
|
|
50
|
-
"jose": "^6.2.12"
|
|
51
|
-
},
|
|
52
|
-
"devDependencies": {
|
|
53
|
-
"@biomejs/biome": "2.5.15",
|
|
54
|
-
"@happy-dom/global-registrator": "^20.14.5",
|
|
55
|
-
"@types/bun": "^1.4.2",
|
|
56
|
-
"@types/react": "^19.3.0",
|
|
57
|
-
"@types/react-dom": "^19.3.0",
|
|
58
|
-
"convex": "^1.46.0",
|
|
59
|
-
"react": "^19.3.0",
|
|
60
|
-
"react-dom": "^19.3.0",
|
|
61
|
-
"typescript": "^7.0.2"
|
|
62
|
-
},
|
|
63
|
-
"publishConfig": {
|
|
64
|
-
"access": "public"
|
|
65
|
-
},
|
|
66
|
-
"keywords": [
|
|
67
|
-
"user-hub",
|
|
68
|
-
"auth",
|
|
69
|
-
"badge",
|
|
70
|
-
"qr",
|
|
71
|
-
"convex",
|
|
72
|
-
"react"
|
|
73
|
-
]
|
|
73
|
+
}
|
|
74
74
|
}
|