@user-hub/auth 0.1.3 → 0.1.4

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 CHANGED
@@ -39,10 +39,10 @@ HUB_ADMIN_PASSWORD=…
39
39
  Then, from your app's folder (`bunx` reads `.env` and `.env.local` automatically; with a secret manager, run it through that):
40
40
 
41
41
  ```sh
42
- bunx hub-user register --name "Your App" --prod https://your-app.com --dev http://localhost:3000
42
+ bunx hub-user register --name "Your App" --new --prod https://your-app.com --dev http://localhost:3000
43
43
  ```
44
44
 
45
- It prints your **app key** (made from 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 `--name` to update the URLs; a URL left out keeps the registered one. Give every app its own name: the same name updates the same app. (A hub admin can do the same under **Apps** in the hub, and copy the values from **Setup**.)
45
+ It prints your **app key** (made from 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 `--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. (A hub admin can do the same under **Apps** in the hub, and copy the values from **Setup**.)
46
46
 
47
47
  ### 3. Set the values
48
48
 
@@ -171,7 +171,7 @@ Apps without Convex use `HubAuthProvider` and `HubSignedIn` from `@user-hub/auth
171
171
  | both | `HubSignedIn` | Shows its children only when signed in. Props: `signedOut?`, `loading?` |
172
172
  | | `useHubUser()`, `useHubToken()`, `useHubSignOut()` | The person (or null), the raw token (or null), a sign-out function. Also from `/convex-react` |
173
173
  | `@user-hub/auth/server` | `verifyHubToken(requestOrToken, { app })` | Checks a token anywhere else |
174
- | CLI | `hub-user register --name <name> [--prod <url>] [--dev <url>] [--hub <url>] [--json]` | Registers or updates the app (at least one URL) |
174
+ | CLI | `hub-user register --name <name> [--prod <url>] [--dev <url>] [--new] [--hub <url>] [--json]` | Registers or updates the app (at least one URL); `--new` only creates, exit code 3 if the name is taken |
175
175
 
176
176
  ## Developing this package
177
177
 
package/dist/cli.js CHANGED
@@ -2,12 +2,14 @@
2
2
 
3
3
  // src/cli.ts
4
4
  import { parseArgs } from "node:util";
5
- var USAGE = `Usage: hub-user register --name <name> [--prod <url>] [--dev <url>] [--hub <url>] [--json]
5
+ var USAGE = `Usage: hub-user register --name <name> [--prod <url>] [--dev <url>] [--new] [--hub <url>] [--json]
6
6
 
7
7
  --name The app's name in the hub; its key comes from it (required)
8
8
  --prod Where badge scans send people. Leave it out until the app is
9
9
  deployed: it can be developed, but not scheduled.
10
10
  --dev Where developers open the app as a person, e.g. http://localhost:3000
11
+ --new Only create a new app: if the name is taken, change nothing and
12
+ exit with code 3 so you can pick another name
11
13
  --hub The hub's URL (default: HUB_URL)
12
14
  --json Print JSON instead of text
13
15
 
@@ -30,6 +32,7 @@ var { values, positionals } = parseArgs({
30
32
  name: { type: "string" },
31
33
  hub: { type: "string" },
32
34
  json: { type: "boolean", default: false },
35
+ new: { type: "boolean", default: false },
33
36
  help: { type: "boolean", short: "h", default: false }
34
37
  }
35
38
  });
@@ -57,10 +60,18 @@ var response = await fetch(`${hub}/api/apps/register`, {
57
60
  body: JSON.stringify({
58
61
  name,
59
62
  ...values.prod !== undefined && { url: values.prod },
60
- ...values.dev !== undefined && { devUrl: values.dev }
63
+ ...values.dev !== undefined && { devUrl: values.dev },
64
+ ...values.new && { createOnly: true }
61
65
  })
62
66
  }).catch((error) => fail(`could not reach ${hub}: ${error.message}`));
63
67
  var result = await response.json().catch(() => ({}));
68
+ if (result.taken) {
69
+ if (values.json)
70
+ console.log(JSON.stringify(result, null, 2));
71
+ else
72
+ console.error(`hub-user: ${result.error}`);
73
+ process.exit(3);
74
+ }
64
75
  if (!response.ok) {
65
76
  fail(result.error ?? `the hub answered ${response.status}`);
66
77
  }
@@ -13,11 +13,20 @@ import { useMemo } from "react";
13
13
  import { jsx } from "react/jsx-runtime";
14
14
  function useConvexHubAuth() {
15
15
  const session = useHubSession2();
16
+ const signOut = useHubSignOut2();
16
17
  return useMemo(() => ({
17
18
  isLoading: session === undefined,
18
19
  isAuthenticated: Boolean(session),
19
- fetchAccessToken: async () => session && session.user.expiresAt > Date.now() ? session.token : null
20
- }), [session]);
20
+ fetchAccessToken: async ({
21
+ forceRefreshToken
22
+ }) => {
23
+ if (forceRefreshToken) {
24
+ signOut();
25
+ return null;
26
+ }
27
+ return session && session.user.expiresAt > Date.now() ? session.token : null;
28
+ }
29
+ }), [session, signOut]);
21
30
  }
22
31
  function ConvexHubAuthProvider({
23
32
  client,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@user-hub/auth",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Sign people in from User Hub badges: Convex auth config, React provider, server verification and the hub-user CLI.",
5
5
  "type": "module",
6
6
  "files": [
@@ -15,7 +15,7 @@ If there is no app yet, don't build one by hand: scaffold one with sign-in alrea
15
15
  bun create @user-hub my-app
16
16
  ```
17
17
 
18
- In a terminal it asks for the hub's URL and admin password and saves them to the new app's `.env.local`. As an agent you never handle the password, so either ask the human to run it, or run it only when `HUB_URL` and `HUB_ADMIN_PASSWORD` are already in the environment (for example `infisical run -- bun create @user-hub my-app`). Never pass `--admin-password` with a value you typed yourself.
18
+ In a terminal it asks for the hub's URL and admin password and saves them to the new app's `.env.local`. As an agent you never handle the password, so either ask the human to run it, or run it only when `HUB_URL` and `HUB_ADMIN_PASSWORD` are already in the environment (for example `infisical run -- bun create @user-hub my-app`).
19
19
 
20
20
  Then:
21
21
 
@@ -58,9 +58,11 @@ You need four things. Ask for them; never guess.
58
58
  2. **Register the app** from the app's repo root, with `HUB_URL` and the password supplied the way the human chose in question 3 above:
59
59
 
60
60
  ```sh
61
- bunx hub-user register --name "<app name>" --prod https://app.example.com --dev http://localhost:3000
61
+ bunx hub-user register --name "<app name>" --new --prod https://app.example.com --dev http://localhost:3000
62
62
  ```
63
63
 
64
+ `--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". Then ask the human for another name and run it again. Drop `--new` only when updating this app's own registration later.
65
+
64
66
  Leave out `--prod` if the app is not deployed yet. Running it again with the same `--name` updates the URLs; a URL left out keeps the registered one. If it fails with "set HUB_URL", "set HUB_ADMIN_PASSWORD" or a wrong password, stop and ask the human; do not work around it.
65
67
 
66
68
  It prints the app **key** (made from the name), `HUB_ISSUER` and `HUB_JWKS` (server side) and `VITE_HUB_URL` (frontend). Add `--json` to parse them.