@cubicecho/server-kit 0.1.0 → 0.2.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.
Files changed (45) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +0 -0
  3. package/_agents/docs/01-database.md +0 -0
  4. package/_agents/docs/02-columns.md +0 -0
  5. package/_agents/docs/03-testing.md +0 -0
  6. package/_agents/docs/04-auth.md +41 -0
  7. package/_agents/docs/common-mistakes.md +0 -0
  8. package/dist/auth/defaults.d.ts +20 -0
  9. package/dist/auth/defaults.js +12 -0
  10. package/dist/auth/index.d.ts +3 -0
  11. package/dist/auth/index.js +3 -0
  12. package/dist/auth/plugins.d.ts +1270 -0
  13. package/dist/auth/plugins.js +39 -0
  14. package/dist/auth/preset.d.ts +53 -0
  15. package/dist/auth/preset.js +21 -0
  16. package/dist/columns/index.d.ts +0 -0
  17. package/dist/columns/index.js +0 -0
  18. package/dist/consts.d.ts +0 -0
  19. package/dist/consts.js +0 -0
  20. package/dist/db/connection.d.ts +0 -0
  21. package/dist/db/connection.js +0 -0
  22. package/dist/db/create.d.ts +0 -0
  23. package/dist/db/create.js +0 -0
  24. package/dist/db/defaults.d.ts +0 -0
  25. package/dist/db/defaults.js +0 -0
  26. package/dist/db/errors.d.ts +0 -0
  27. package/dist/db/errors.js +0 -0
  28. package/dist/db/index.d.ts +0 -0
  29. package/dist/db/index.js +0 -0
  30. package/dist/db/prepare.d.ts +0 -0
  31. package/dist/db/prepare.js +0 -0
  32. package/dist/db/ssl.d.ts +0 -0
  33. package/dist/db/ssl.js +0 -0
  34. package/dist/db/version.d.ts +0 -0
  35. package/dist/db/version.js +0 -0
  36. package/dist/db/wait.d.ts +0 -0
  37. package/dist/db/wait.js +0 -0
  38. package/dist/testing/index.d.ts +0 -0
  39. package/dist/testing/index.js +0 -0
  40. package/dist/testing/postgres-problems.d.ts +0 -0
  41. package/dist/testing/postgres-problems.js +0 -0
  42. package/dist/testing/schema-problems.d.ts +0 -0
  43. package/dist/testing/schema-problems.js +0 -0
  44. package/llms.txt +16 -1
  45. package/package.json +13 -1
@@ -0,0 +1,39 @@
1
+ import { oidcPairingPlugins } from '@cubicecho/better-auth-oidc-pairing';
2
+ import { bearer, magicLink } from 'better-auth/plugins';
3
+ import { AUTH_DEFAULTS, AUTH_PATHS } from './defaults.js';
4
+ const SECONDS_PER_MINUTE = 60;
5
+ /** better-auth's id for the magic-link plugin. */
6
+ const MAGIC_LINK_PLUGIN_ID = 'magic-link';
7
+ /**
8
+ * How people sign in: bearer sessions, OIDC with pairing, and emailed links. Never a password.
9
+ *
10
+ * @param options - The app's links and provider.
11
+ * @returns The plugins, to spread into `plugins`.
12
+ */
13
+ export function signInPlugins({ appUrl, deliver, oidc, paths, magicLinkTtlMinutes = AUTH_DEFAULTS.magicLinkTtlMinutes, }) {
14
+ const { login, verify, signedIn } = { ...AUTH_PATHS, ...paths };
15
+ const links = deliver === undefined
16
+ ? []
17
+ : [
18
+ magicLink({
19
+ expiresIn: magicLinkTtlMinutes * SECONDS_PER_MINUTE,
20
+ storeToken: 'hashed',
21
+ // Links go to the app, which spends the token over GraphQL. The REST verify route is not mounted.
22
+ sendMagicLink: ({ email, token }) => deliver({ email, token, url: `${appUrl}${verify}?token=${encodeURIComponent(token)}` }),
23
+ }),
24
+ ];
25
+ return [
26
+ bearer(),
27
+ ...oidcPairingPlugins({ ...oidc, signedInURL: `${appUrl}${signedIn}`, errorURL: `${appUrl}${login}` }),
28
+ ...links,
29
+ ];
30
+ }
31
+ /**
32
+ * Whether an instance emails sign-in links.
33
+ *
34
+ * @param auth - The auth instance.
35
+ * @returns true when it was built with a way to deliver them.
36
+ */
37
+ export function sendsMagicLinks(auth) {
38
+ return (auth.options.plugins ?? []).some((plugin) => plugin.id === MAGIC_LINK_PLUGIN_ID);
39
+ }
@@ -0,0 +1,53 @@
1
+ import { drizzleAdapter } from 'better-auth/adapters/drizzle';
2
+ import { type AuthPaths, type AuthSettings } from './defaults.ts';
3
+ /** The tables better-auth keeps, by its own model names. */
4
+ export interface AuthTables {
5
+ user: unknown;
6
+ session: unknown;
7
+ account: unknown;
8
+ verification: unknown;
9
+ /** Tables plugins add, such as `apikey` and `ssoProvider`. */
10
+ [model: string]: unknown;
11
+ }
12
+ /** What an app gives the preset. */
13
+ export interface AuthPresetOptions extends Partial<Pick<AuthSettings, 'sessionTtlDays'>> {
14
+ /** The Drizzle client. */
15
+ db: Parameters<typeof drizzleAdapter>[0];
16
+ /** The tables, passed explicitly: the rc Drizzle client is keyed by relations, so they cannot be discovered. */
17
+ tables: AuthTables;
18
+ /** What better-auth signs with. */
19
+ secret: string;
20
+ /** Where a browser reaches this server. */
21
+ serverUrl: string;
22
+ /** Where users reach the app. A sign-in may finish there. */
23
+ appUrl: string;
24
+ /**
25
+ * Pages that are somewhere else in this app.
26
+ *
27
+ * @defaultValue `AUTH_PATHS`
28
+ */
29
+ paths?: Partial<AuthPaths>;
30
+ }
31
+ /**
32
+ * The better-auth options every server shares. Spread it into `betterAuth`, then add plugins and hooks.
33
+ *
34
+ * @param options - What differs per app.
35
+ * @returns Sessions in Postgres, ids from the columns' own defaults, and the app's origin trusted.
36
+ */
37
+ export declare function authPreset({ db, tables, secret, serverUrl, appUrl, paths, sessionTtlDays, }: AuthPresetOptions): {
38
+ database: (options: import("@better-auth/core").BetterAuthOptions) => import("@better-auth/core/db/adapter").DBAdapter<import("@better-auth/core").BetterAuthOptions>;
39
+ secret: string;
40
+ baseURL: string;
41
+ trustedOrigins: string[];
42
+ advanced: {
43
+ database: {
44
+ generateId: 'uuid';
45
+ };
46
+ };
47
+ session: {
48
+ expiresIn: number;
49
+ };
50
+ onAPIError: {
51
+ errorURL: string;
52
+ };
53
+ };
@@ -0,0 +1,21 @@
1
+ import { drizzleAdapter } from 'better-auth/adapters/drizzle';
2
+ import { AUTH_DEFAULTS, AUTH_PATHS } from './defaults.js';
3
+ const SECONDS_PER_DAY = 86_400;
4
+ /**
5
+ * The better-auth options every server shares. Spread it into `betterAuth`, then add plugins and hooks.
6
+ *
7
+ * @param options - What differs per app.
8
+ * @returns Sessions in Postgres, ids from the columns' own defaults, and the app's origin trusted.
9
+ */
10
+ export function authPreset({ db, tables, secret, serverUrl, appUrl, paths, sessionTtlDays = AUTH_DEFAULTS.sessionTtlDays, }) {
11
+ return {
12
+ database: drizzleAdapter(db, { provider: 'pg', schema: tables }),
13
+ secret,
14
+ baseURL: serverUrl,
15
+ trustedOrigins: [new URL(appUrl).origin],
16
+ // On Postgres this leaves the id out of the insert, so the column's uuidv7() fills it.
17
+ advanced: { database: { generateId: 'uuid' } },
18
+ session: { expiresIn: sessionTtlDays * SECONDS_PER_DAY },
19
+ onAPIError: { errorURL: `${appUrl}${{ ...AUTH_PATHS, ...paths }.login}` },
20
+ };
21
+ }
File without changes
File without changes
package/dist/consts.d.ts CHANGED
File without changes
package/dist/consts.js CHANGED
File without changes
File without changes
File without changes
File without changes
package/dist/db/create.js CHANGED
File without changes
File without changes
File without changes
File without changes
package/dist/db/errors.js CHANGED
File without changes
File without changes
package/dist/db/index.js CHANGED
File without changes
File without changes
File without changes
package/dist/db/ssl.d.ts CHANGED
File without changes
package/dist/db/ssl.js CHANGED
File without changes
File without changes
File without changes
package/dist/db/wait.d.ts CHANGED
File without changes
package/dist/db/wait.js CHANGED
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/llms.txt CHANGED
@@ -4,15 +4,30 @@
4
4
 
5
5
  Agent-facing docs for @cubicecho/server-kit: one task or concept per page, then what each package.json "exports" entry exports. Generated from _agents/docs/ and the doc comments in src/ by @cubicecho/agent-docs; edit those, not this file.
6
6
 
7
- Requires Node >=22. Peer dependencies: `@electric-sql/pglite` ^0.5.0 (optional), `drizzle-orm` 1.0.0-rc.5, `postgres` ^3.4.0, `vitest` ^5.0.0 (optional).
7
+ Requires Node >=22. Peer dependencies: `@better-auth/sso` >=1.7.28 <1.8.0, `@electric-sql/pglite` ^0.5.0 (optional), `better-auth` >=1.7.28 <1.8.0, `drizzle-orm` 1.0.0-rc.5, `postgres` ^3.4.0, `vitest` ^5.0.0 (optional).
8
8
 
9
9
  ## Docs
10
10
 
11
11
  - [Database boot](_agents/docs/01-database.md): `@cubicecho/server-kit/db` makes a Postgres database ready to migrate: it waits for the server, creates the database when it is missing, and refuses a server the app cannot run on.
12
12
  - [Columns](_agents/docs/02-columns.md): `@cubicecho/server-kit/columns` has the Drizzle columns that keep a table to the conventions: a UUIDv7 id, moments with a time zone, and a table limited to one row.
13
13
  - [Test checks](_agents/docs/03-testing.md): `@cubicecho/server-kit/testing` registers Vitest tests that hold an app to the backend standard: its tables, and the Postgres it runs.
14
+ - [Auth preset](_agents/docs/04-auth.md): `@cubicecho/server-kit/auth` has the better-auth options and sign-in plugins every server shares: sessions in Postgres, bearer tokens, emailed links, and OIDC with pairing. The app still calls `betterAuth`, so it adds its own plugins and hooks and keeps exact types.
14
15
  - [Common mistakes](_agents/docs/common-mistakes.md): What goes wrong when the kit is wired up slightly differently, and the fix for each.
15
16
 
17
+ ## Exports: @cubicecho/server-kit/auth
18
+
19
+ - `AUTH_DEFAULTS`: What `authPreset` and `signInPlugins` use when the app overrides nothing.
20
+ - `AUTH_PATHS`: Where those pages are when the app overrides nothing.
21
+ - `AuthPaths` (type): The app's pages a sign-in passes through, as paths under `appUrl`.
22
+ - `AuthSettings` (type): Session and link lifetimes.
23
+ - `MagicLink` (type): What delivering one sign-in link is given.
24
+ - `SignInOptions` (type): What an app gives `signInPlugins`.
25
+ - `sendsMagicLinks`: Whether an instance emails sign-in links.
26
+ - `signInPlugins`: How people sign in: bearer sessions, OIDC with pairing, and emailed links.
27
+ - `AuthPresetOptions` (type): What an app gives the preset.
28
+ - `AuthTables` (type): The tables better-auth keeps, by its own model names.
29
+ - `authPreset`: The better-auth options every server shares.
30
+
16
31
  ## Exports: @cubicecho/server-kit/columns
17
32
 
18
33
  - `id`: The primary key of an entity: a UUIDv7, made by the database.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cubicecho/server-kit",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "The server code every cubicecho GraphQL backend shares: database boot, id columns and the checks that keep a schema to the standard.",
5
5
  "license": "MIT",
6
6
  "author": "Benjamin Van Treese",
@@ -23,6 +23,10 @@
23
23
  "server"
24
24
  ],
25
25
  "exports": {
26
+ "./auth": {
27
+ "types": "./dist/auth/index.d.ts",
28
+ "import": "./dist/auth/index.js"
29
+ },
26
30
  "./columns": {
27
31
  "types": "./dist/columns/index.d.ts",
28
32
  "import": "./dist/columns/index.js"
@@ -55,7 +59,9 @@
55
59
  "test": "vitest run"
56
60
  },
57
61
  "peerDependencies": {
62
+ "@better-auth/sso": ">=1.7.28 <1.8.0",
58
63
  "@electric-sql/pglite": "^0.5.0",
64
+ "better-auth": ">=1.7.28 <1.8.0",
59
65
  "drizzle-orm": "1.0.0-rc.5",
60
66
  "postgres": "^3.4.0",
61
67
  "vitest": "^5.0.0"
@@ -69,6 +75,7 @@
69
75
  }
70
76
  },
71
77
  "devDependencies": {
78
+ "@better-auth/sso": "^1.7.28",
72
79
  "@biomejs/biome": "^2.6.2",
73
80
  "@cubicecho/agent-docs": "^0.2.0",
74
81
  "@electric-sql/pglite": "^0.5.17",
@@ -79,11 +86,16 @@
79
86
  "@semantic-release/npm": "^13.2.0",
80
87
  "@semantic-release/release-notes-generator": "^14.1.1",
81
88
  "@types/node": "^24.19.4",
89
+ "better-auth": "^1.7.28",
82
90
  "drizzle-kit": "^1.0.0-rc.5",
83
91
  "drizzle-orm": "^1.0.0-rc.5",
92
+ "graphql": "^16.15.0",
84
93
  "postgres": "^3.4.10",
85
94
  "semantic-release": "^25.0.9",
86
95
  "typescript": "^7.0.2",
87
96
  "vitest": "^5.1.2"
97
+ },
98
+ "dependencies": {
99
+ "@cubicecho/better-auth-oidc-pairing": "^1.1.0"
88
100
  }
89
101
  }