@user-hub/auth 0.6.0 → 0.6.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/README.md
CHANGED
|
@@ -118,6 +118,8 @@ const { hubToken } = Route.useRouteContext();
|
|
|
118
118
|
|
|
119
119
|
Apps that load Convex queries on the server through TanStack Query (`ConvexQueryClient`) also give it the token in `beforeLoad`, so those queries run as the person: `if (hubToken) context.convexQueryClient.serverHttpClient?.setAuth(hubToken)`.
|
|
120
120
|
|
|
121
|
+
With Convex, create the client so queries wait for the person's token and keep it, instead of running signed out first and then signing in twice: `new ConvexReactClient(url, { expectAuth: true, initialAuthTokenReuse: true })`, or the same options as the second argument of `new ConvexQueryClient(url, …)`. On Vite, also pre-bundle Convex and this package together, so the browser holds one `convex/react` ("Could not find Convex client" otherwise): `optimizeDeps: { include: ["convex/react", "@user-hub/auth/convex-react"] }`, plus `"@convex-dev/react-query"` when you use it.
|
|
122
|
+
|
|
121
123
|
When `getHubToken` cannot vouch for a token (none yet, forged, or the hub unreachable) it returns `null` and the page renders as loading, as without it. When it returns one, the page carries that token, so it marks the response `Cache-Control: private, no-store` and `Vary: Cookie`: never put such pages behind a shared cache.
|
|
122
124
|
|
|
123
125
|
### 5. Use the person
|
package/dist/convex-react.js
CHANGED
|
@@ -103,6 +103,16 @@ var MAX_TIMEOUT_MS = 2 ** 31 - 1;
|
|
|
103
103
|
var REFRESH_AFTER_MS = 5 * 60 * 1000;
|
|
104
104
|
var REFRESH_BEFORE_EXPIRY_MS = 60 * 1000;
|
|
105
105
|
var RETRY_MS = 30 * 1000;
|
|
106
|
+
var REFRESHED_LIFETIME_MS = 15 * 60 * 1000;
|
|
107
|
+
var CLOCK_TOLERANCE_MS = 30 * 1000;
|
|
108
|
+
function recentlyRefreshed(token) {
|
|
109
|
+
const issuedAt = refreshInfo(token)?.issuedAt;
|
|
110
|
+
const expiresAt = readClaims(token)?.expiresAt;
|
|
111
|
+
if (!issuedAt || !expiresAt)
|
|
112
|
+
return false;
|
|
113
|
+
const age = Date.now() - issuedAt;
|
|
114
|
+
return expiresAt - issuedAt <= REFRESHED_LIFETIME_MS && age > -CLOCK_TOLERANCE_MS && age < REFRESH_AFTER_MS;
|
|
115
|
+
}
|
|
106
116
|
var FRESH_MS = 5 * 1000;
|
|
107
117
|
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;
|
|
108
118
|
var HubContext = createContext({
|
|
@@ -209,7 +219,7 @@ function HubSessionProvider({
|
|
|
209
219
|
const valid = sessionFor(fromUrl, app, hubNow()) ?? newestSession(sessionFor(initial, app, hubNow()), sessionFor(stored, app, hubNow()));
|
|
210
220
|
if (valid) {
|
|
211
221
|
signIn(valid);
|
|
212
|
-
if (refreshUrlFor(valid.token, app, hubUrl)) {
|
|
222
|
+
if (refreshUrlFor(valid.token, app, hubUrl) && (valid.token === fromUrl || !recentlyRefreshed(valid.token))) {
|
|
213
223
|
trade(valid.token);
|
|
214
224
|
}
|
|
215
225
|
return;
|
package/dist/react.js
CHANGED
package/package.json
CHANGED
|
@@ -36,7 +36,7 @@ The hub's URL also comes from the human. It goes in three places with the same v
|
|
|
36
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
37
|
- **Check access on the server**: `requireHubUser(ctx)` / `requireFacilitator(ctx)` in Convex, `verifyHubToken` elsewhere. `useHubUser()` is for display; hiding a page protects nothing.
|
|
38
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.
|
|
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. With Convex, create the client with `expectAuth: true` and `initialAuthTokenReuse: true`.
|
|
40
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
41
|
- **No other sign-in.** No login screens, passwords or sessions. If something is missing, it belongs in `@user-hub/auth`.
|
|
42
42
|
- **Secrets stay out of chat and files you write.** The API key lives only in `.env.local`, saved by `user-hub login`.
|
|
@@ -73,7 +73,9 @@ const { hubToken } = Route.useRouteContext();
|
|
|
73
73
|
<ConvexHubAuthProvider client={convex} app="<key>" hubUrl={import.meta.env.VITE_HUB_URL} initialToken={hubToken}>
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
-
With `ConvexQueryClient` (Convex through TanStack Query), also call `context.convexQueryClient.serverHttpClient?.setAuth(hubToken)` in `beforeLoad` when there is a token, so server-side queries run as the person.
|
|
76
|
+
With `ConvexQueryClient` (Convex through TanStack Query), also call `context.convexQueryClient.serverHttpClient?.setAuth(hubToken)` in `beforeLoad` when there is a token, so server-side queries run as the person.
|
|
77
|
+
|
|
78
|
+
With Convex, create the client so queries wait for the person's token and keep it, instead of running signed out first and then signing in twice: `new ConvexReactClient(url, { expectAuth: true, initialAuthTokenReuse: true })`, or the same options as the second argument of `new ConvexQueryClient(url, …)`. On Vite, also pre-bundle Convex and this package together, so the browser holds one `convex/react` ("Could not find Convex client" otherwise): `optimizeDeps: { include: ["convex/react", "@user-hub/auth/convex-react"] }`, plus `"@convex-dev/react-query"` when you use it. Never read or set the `user-hub-token.<key>` cookie yourself: the provider writes it and `getHubToken` reads it.
|
|
77
79
|
|
|
78
80
|
## 5. Use the person
|
|
79
81
|
|