@jgb-solutions/kit 0.2.0 → 0.2.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/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  All notable changes to `@jgb-solutions/kit`. Versions follow semver.
4
4
 
5
+ ## 0.2.1
6
+
7
+ - `standardAuthOptions({ passkeyOrigin: 'request' })`: passkeys checked against the request's own origin, which `trustedBaseUrl` limits to the site and local hosts, so the site, previews and any local port each work (the jgb-starter template's setup). The default, `'listed'`, keeps the fixed list (the site, `localOrigins`, the Android app).
8
+ - README: the server call is `auth.api.linkSocialAccount` (the endpoint is `/link-social`).
9
+ - `authSettings`: `E2E=1` is refused only on a production server that listens beyond this machine (`NODE_ENV=production` and `HOST` not a loopback address). 0.2.0 refused it whenever `NODE_ENV` was production, which also stopped E2E servers that run the production build on `127.0.0.1` (the jgb-starter Bun stack).
10
+
5
11
  ## 0.2.0
6
12
 
7
13
  - `@jgb-solutions/kit/auth`: Better Auth set up the same way in every app. Pieces (`authSettings`, `trustedBaseUrl`, `passkeyOrigins`, `androidOrigin`, `assetLinks`, `RATE_LIMIT_RULES`, `counterRateLimit`, `googleProvider`, `CONFIRMED_ACCOUNT_LINKING`, `freeUsername`, `afterAccountSaved`, `pictureFromIdToken`, `signedInFlagHook`, `asPublicRequest`, `toAuthUser`) and the preset `standardAuthOptions`: username or email with a password, passkeys, Google that joins only confirmed accounts (connect Google from the account page otherwise), roles and the first admins (`ADMIN_EMAILS`, confirmed addresses only), Bearer sessions for the mobile app, rate limits, no email ever sent.
package/README.md CHANGED
@@ -79,7 +79,7 @@ What the preset sets:
79
79
 
80
80
  - **Passwords:** 12 to 128 characters, with no email verification and no reset link (no email is ever sent). `SIGN_UP=off` closes sign-up; existing accounts still sign in.
81
81
  - **Usernames** (Better Auth's username plugin): 3 to 30 characters, lower-case letters, digits, dots and underscores. An account made without one (Google) gets one from its name, with a number when it's taken.
82
- - **Passkeys** for the site, the `localOrigins` and the Android app (`androidCertSha256`).
82
+ - **Passkeys** for the site, the `localOrigins` and the Android app (`androidCertSha256`). With `passkeyOrigin: 'request'`, the request's own origin instead (the site, previews and any local port; one of `trustedBaseUrl`'s hosts), for apps without an Android app.
83
83
  - **Google**, when `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET` are set. `GOOGLE_MOBILE_CLIENT_IDS` adds the apps' clients, for ID-token sign-in. A new Google address makes a new account with a confirmed address. Joining existing accounts is "merge only confirmed" (see below).
84
84
  - **Roles:** Better Auth's admin plugin, with the app's access control. A new account whose address is in `ADMIN_EMAILS` starts as admin when it's created with Google (a confirmed address); a sign-up with that address by email and password starts as an ordinary user, so nobody can claim an admin's address first. Other first admins are promoted from the server (the app's admin script).
85
85
  - **The mobile app:** the bearer plugin.
@@ -91,7 +91,7 @@ What the preset sets:
91
91
  The app never sends email, so an address typed at sign-up is never confirmed. Someone could sign up first with another person's address and a password they know. So **"Continue with Google" joins an existing account with the same address only when that account's address is confirmed**: it was made with Google, or by an admin. Otherwise Better Auth refuses, and the app shows the way in:
92
92
 
93
93
  1. **On the sign-in page**, the refusal arrives as `?error=account_not_linked` after Google's redirect, or as an error body from an ID-token sign-in. `authErrorCode(answer)` (`kit/auth/rules`) turns either into `ACCOUNT_NOT_LINKED`. The page says: _An account with this address already exists. Sign in with your password, then connect Google from your account page._
94
- 2. **On the account page**, "Connect Google" calls Better Auth's `linkSocial` for the signed-in person: `auth.api.linkSocial({ body: { provider: 'google', callbackURL: '/account', errorCallbackURL: '/account' }, headers })` on the website (go to the `url` it returns), or `auth.linkGoogle(idToken)` in the app. The Google address may differ from the account's (username accounts have a placeholder address). The account's other sessions end, this one stays, and the account takes Google's picture if it had none. From then on, "Continue with Google" signs into it.
94
+ 2. **On the account page**, "Connect Google" calls Better Auth's `/link-social` for the signed-in person: `auth.api.linkSocialAccount({ body: { provider: 'google', callbackURL: '/account', errorCallbackURL: '/account' }, headers })` on the website (go to the `url` it returns), or `auth.linkGoogle(idToken)` in the app. The Google address may differ from the account's (username accounts have a placeholder address). The account's other sessions end, this one stays, and the account takes Google's picture if it had none. From then on, "Continue with Google" signs into it.
95
95
  3. A Google account that already belongs to another account gives `GOOGLE_ALREADY_LINKED`. A Google address Google didn't verify gives `GOOGLE_EMAIL_NOT_CONFIRMED`.
96
96
 
97
97
  When Google joins a confirmed account that also has a password, that account's other sessions end too.
package/dist/auth.d.ts CHANGED
@@ -22,8 +22,9 @@ export type AuthSettings = {
22
22
  * Sign-in settings from the environment, the same on every host: BETTER_AUTH_SECRET, SIGN_UP,
23
23
  * ADMIN_EMAILS, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET and GOOGLE_MOBILE_CLIENT_IDS (comma-separated:
24
24
  * the Android and iOS clients). E2E=1 marks the E2E test server: its stand-in secret and its rate
25
- * limits off are refused when NODE_ENV is production, so a stray E2E=1 can't start production with a
26
- * public secret. Throws without a secret.
25
+ * limits off are refused on a production server that listens beyond this machine (NODE_ENV=production
26
+ * with HOST unset or not a loopback address), so a stray E2E=1 can't start production with a public
27
+ * secret. E2E servers run a production build on 127.0.0.1. Throws without a secret.
27
28
  */
28
29
  export declare function authSettings(env: Env): AuthSettings;
29
30
  /**
@@ -254,7 +255,14 @@ export type StandardAuthInput<A extends AdminOptions> = {
254
255
  takenUsernames: (candidates: string[]) => Promise<ReadonlySet<string>>;
255
256
  /** The username base when a name gives too little ("listener", "member"…). */
256
257
  usernameFallback: string;
257
- /** Local origins allowed to use passkeys (development and E2E): `['http://localhost:5173']`. */
258
+ /**
259
+ * Which origins may use passkeys. `'listed'` (the default): the site, `localOrigins` and the Android
260
+ * app (`androidCertSha256`). `'request'`: the request's own origin, which `trustedBaseUrl` limits to
261
+ * the site and local hosts, so the site, previews and any local port work, each with its own
262
+ * passkeys. `'request'` can't serve the Android app.
263
+ */
264
+ passkeyOrigin?: 'listed' | 'request';
265
+ /** Local origins allowed to use passkeys (development and E2E) with `passkeyOrigin: 'listed'`: `['http://localhost:5173']`. */
258
266
  localOrigins?: readonly string[];
259
267
  /** The Android app's signing certificates (SHA-256, `AB:CD:…`), for passkeys in the app. */
260
268
  androidCertSha256?: readonly string[];
@@ -339,17 +347,7 @@ export declare function standardAuthOptions<A extends AdminOptions>(input: Stand
339
347
  email: string;
340
348
  emailVerified: boolean;
341
349
  name: string;
342
- image
343
- /**
344
- * Better Auth's `baseURL`: the request's own host, but only the site's or a local one; anything else
345
- * falls back to the site. A forged Host header can't choose the passkeys' relying party or the
346
- * address in a callback.
347
- */
348
- ? /**
349
- * Better Auth's `baseURL`: the request's own host, but only the site's or a local one; anything else
350
- * falls back to the site. A forged Host header can't choose the passkeys' relying party or the
351
- * address in a callback.
352
- */: string | null | undefined;
350
+ image?: string | null | undefined;
353
351
  }> & Record<string, unknown>, context: import("better-auth").GenericEndpointContext | null): Promise<{
354
352
  data: {
355
353
  displayUsername?: string | undefined;
@@ -1617,7 +1615,7 @@ export declare function standardAuthOptions<A extends AdminOptions>(input: Stand
1617
1615
  userId: string;
1618
1616
  expiresAt: Date;
1619
1617
  token: string;
1620
- ipAddress? /** Local origins allowed to use passkeys (development and E2E): `['http://localhost:5173']`. */: string | null | undefined;
1618
+ ipAddress?: string | null | undefined;
1621
1619
  userAgent?: string | null | undefined;
1622
1620
  };
1623
1621
  };
package/dist/auth.js CHANGED
@@ -94,8 +94,9 @@ function authErrorCode(answer) {
94
94
  var E2E_SECRET = "e2e-only-secret-never-used-in-production-0000";
95
95
  function authSettings(env) {
96
96
  const e2e = env.E2E === "1";
97
- if (e2e && env.NODE_ENV === "production")
98
- throw new Error("E2E=1 is set in production: remove it.");
97
+ const loopback = ["127.0.0.1", "::1", "localhost"].includes((env.HOST ?? "").trim().toLowerCase());
98
+ if (e2e && env.NODE_ENV === "production" && !loopback)
99
+ throw new Error("E2E=1 is set on a production server that listens beyond this machine: remove it.");
99
100
  const secret = env.BETTER_AUTH_SECRET || (e2e ? E2E_SECRET : "");
100
101
  if (!secret)
101
102
  throw new Error("BETTER_AUTH_SECRET is not set.");
@@ -254,6 +255,9 @@ function toAuthUser(user, roleOf) {
254
255
  }
255
256
  function standardAuthOptions(input) {
256
257
  const { settings } = input;
258
+ const fromRequest = input.passkeyOrigin === "request";
259
+ if (fromRequest && (input.androidCertSha256?.length || input.localOrigins?.length))
260
+ throw new Error("passkeyOrigin 'request' can't take androidCertSha256 or localOrigins: use 'listed'.");
257
261
  const days = input.sessionDays ?? 30;
258
262
  return {
259
263
  appName: input.appName,
@@ -311,10 +315,12 @@ function standardAuthOptions(input) {
311
315
  username({ minUsernameLength: USERNAME.min, maxUsernameLength: USERNAME.max }),
312
316
  passkey({
313
317
  rpName: input.appName,
314
- origin: passkeyOrigins(input.siteUrl, {
315
- ...input.localOrigins ? { local: input.localOrigins } : {},
316
- ...input.androidCertSha256 ? { androidCertSha256: input.androidCertSha256 } : {}
317
- })
318
+ ...fromRequest ? {} : {
319
+ origin: passkeyOrigins(input.siteUrl, {
320
+ ...input.localOrigins ? { local: input.localOrigins } : {},
321
+ ...input.androidCertSha256 ? { androidCertSha256: input.androidCertSha256 } : {}
322
+ })
323
+ }
318
324
  }),
319
325
  admin(input.admin),
320
326
  bearer(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jgb-solutions/kit",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Generic plumbing for web apps on Bun and Cloudflare Workers: small, typed, tree-shakable modules (Cloudflare Access, Better Auth).",
5
5
  "license": "MIT",
6
6
  "author": "JGB Solutions",