create-protobase 0.47.1 → 0.49.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/dist/env-file.js CHANGED
@@ -13,10 +13,10 @@ const withDatabasePort = (example, port) => {
13
13
  url.port = String(port);
14
14
  return replaceSetting(example, "DATABASE_URL", url.toString());
15
15
  };
16
- /** A new project's .env: its .env.example with `secret` as BETTER_AUTH_SECRET in place of the placeholder. */
16
+ /** A new project's .env: its .env.example with `secret` as PROTOBASE_SECRET in place of the placeholder. */
17
17
  const envFile = (example, secret) => {
18
- setting(example, "BETTER_AUTH_SECRET");
19
- return replaceSetting(example, "BETTER_AUTH_SECRET", secret);
18
+ setting(example, "PROTOBASE_SECRET");
19
+ return replaceSetting(example, "PROTOBASE_SECRET", secret);
20
20
  };
21
21
  //#endregion
22
22
  export { databaseUrl, envFile, withDatabasePort };
@@ -4,11 +4,11 @@ DATABASE_URL=postgres://protobase:protobase@localhost:55432/protobase
4
4
 
5
5
  # Admin store (Better Auth users, sessions, signing keys): the `auth` schema of DATABASE_URL, created by `db:migrate`.
6
6
  # Public URL of the API, for cookies and as the issuer of the login tokens (use the https tunnel URL when tunnelling).
7
- BETTER_AUTH_URL=http://localhost:5173
7
+ PROTOBASE_URL=http://localhost:5173
8
8
  # Signs cookies and encrypts the token keys. Generate your own: openssl rand -base64 32
9
- BETTER_AUTH_SECRET=change-me-generate-with-openssl-rand-base64-32
9
+ PROTOBASE_SECRET=change-me-generate-with-openssl-rand-base64-32
10
10
  # Extra comma separated origins allowed to call the auth endpoints (a dev tunnel needs its exact host here, such as https://my-tunnel.trycloudflare.com).
11
- # TRUSTED_ORIGINS=
11
+ # PROTOBASE_TRUSTED_ORIGINS=
12
12
  # Mail for password reset and emailed sign-in and two-factor codes: the SMTP server (smtp:// with STARTTLS, or smtps://) and the sender. Without them those are off.
13
13
  # PROTOBASE_SMTP_URL=smtp://user:password@localhost:1025
14
14
  # PROTOBASE_MAIL_FROM=noreply@example.com
@@ -0,0 +1,26 @@
1
+ import { sql } from 'kysely'
2
+ import type { AuthHooks } from '@protobase/server'
3
+
4
+ // The ERP's records name their organization by core.organizations' integer id, so a new organization takes the next
5
+ // value of that table's sequence, and gets its row there with the ERP's own settings once it is made.
6
+ export const generateOrganizationId: AuthHooks['generateOrganizationId'] = async ({ db }) => {
7
+ const { rows } = await sql<{ id: string }>`select nextval(pg_get_serial_sequence('core.organizations', 'id'))::text as id`.execute(db)
8
+ return rows[0]!.id
9
+ }
10
+
11
+ export const onOrganizationCreated: AuthHooks['onOrganizationCreated'] = async ({ organization }, { db }) => {
12
+ await sql`insert into core.organizations (id, name, slug, country_code, currency_code) values (${Number(organization.id)}, ${organization.name}, ${organization.slug}, 'NL', 'EUR') on conflict (id) do nothing`.execute(db)
13
+ }
14
+
15
+ export const organizationHooks: AuthHooks['organizationHooks'] = {
16
+ afterUpdateOrganization: async ({ organization }, { db }) => {
17
+ if (organization) await sql`update core.organizations set name = ${organization.name}, slug = ${organization.slug} where id = ${Number(organization.id)}`.execute(db)
18
+ },
19
+ }
20
+
21
+ // Orders name their owner by core.users' integer id, so a new user takes the next value of that table's sequence; their
22
+ // row there comes with their first membership (db/migrations/019_auth_users.sql).
23
+ export const generateUserId: AuthHooks['generateUserId'] = async ({ db }) => {
24
+ const { rows } = await sql<{ id: string }>`select nextval(pg_get_serial_sequence('core.users', 'id'))::text as id`.execute(db)
25
+ return rows[0]!.id
26
+ }
@@ -0,0 +1,297 @@
1
+ -- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
2
+ -- Applied with the project's other migrations; edit or remove statements the project handles differently.
3
+
4
+ -- The record of the auth store's migrations.
5
+ create schema if not exists "auth";
6
+
7
+ create table "auth"."auth_migrations" (
8
+ version integer primary key,
9
+ name text not null,
10
+ applied_at timestamptz default current_timestamp not null
11
+ );
12
+
13
+ -- The auth store an earlier Protobase made, without a record of its migrations, adopted: 1 core, 2 two_factor, 3 passkeys, 4 sign_in_policy, 5 staff_sign_in, 6 organizations
14
+ -- recorded as applied, once every table and column they make is there.
15
+ do $adopt$
16
+ declare
17
+ missing text;
18
+ begin
19
+ select string_agg(expected.table_name || '.' || expected.column_name, ', ' order by expected.table_name, expected.column_name) into missing
20
+ from (values ('account', 'id', 'text'),
21
+ ('account', 'account_id', 'text'),
22
+ ('account', 'provider_id', 'text'),
23
+ ('account', 'user_id', 'text'),
24
+ ('account', 'access_token', 'text'),
25
+ ('account', 'refresh_token', 'text'),
26
+ ('account', 'id_token', 'text'),
27
+ ('account', 'access_token_expires_at', 'timestamptz'),
28
+ ('account', 'refresh_token_expires_at', 'timestamptz'),
29
+ ('account', 'scope', 'text'),
30
+ ('account', 'password', 'text'),
31
+ ('account', 'created_at', 'timestamptz'),
32
+ ('account', 'updated_at', 'timestamptz'),
33
+ ('jwks', 'id', 'text'),
34
+ ('jwks', 'public_key', 'text'),
35
+ ('jwks', 'private_key', 'text'),
36
+ ('jwks', 'created_at', 'timestamptz'),
37
+ ('jwks', 'expires_at', 'timestamptz'),
38
+ ('jwks', 'alg', 'text'),
39
+ ('jwks', 'crv', 'text'),
40
+ ('session', 'id', 'text'),
41
+ ('session', 'expires_at', 'timestamptz'),
42
+ ('session', 'token', 'text'),
43
+ ('session', 'created_at', 'timestamptz'),
44
+ ('session', 'updated_at', 'timestamptz'),
45
+ ('session', 'ip_address', 'text'),
46
+ ('session', 'user_agent', 'text'),
47
+ ('session', 'user_id', 'text'),
48
+ ('session', 'impersonated_by', 'text'),
49
+ ('session', 'active_role', 'text'),
50
+ ('user', 'id', 'text'),
51
+ ('user', 'name', 'text'),
52
+ ('user', 'email', 'text'),
53
+ ('user', 'email_verified', 'bool'),
54
+ ('user', 'image', 'text'),
55
+ ('user', 'created_at', 'timestamptz'),
56
+ ('user', 'updated_at', 'timestamptz'),
57
+ ('user', 'role', 'text'),
58
+ ('user', 'banned', 'bool'),
59
+ ('user', 'ban_reason', 'text'),
60
+ ('user', 'ban_expires', 'timestamptz'),
61
+ ('user', 'last_role', 'text'),
62
+ ('user', 'email_confirmed_at', 'timestamptz'),
63
+ ('verification', 'id', 'text'),
64
+ ('verification', 'identifier', 'text'),
65
+ ('verification', 'value', 'text'),
66
+ ('verification', 'expires_at', 'timestamptz'),
67
+ ('verification', 'created_at', 'timestamptz'),
68
+ ('verification', 'updated_at', 'timestamptz'),
69
+ ('two_factor', 'id', 'text'),
70
+ ('two_factor', 'secret', 'text'),
71
+ ('two_factor', 'backup_codes', 'text'),
72
+ ('two_factor', 'user_id', 'text'),
73
+ ('two_factor', 'verified', 'bool'),
74
+ ('two_factor', 'failed_verification_count', 'int4'),
75
+ ('two_factor', 'locked_until', 'timestamptz'),
76
+ ('user', 'two_factor_enabled', 'bool'),
77
+ ('passkey', 'id', 'text'),
78
+ ('passkey', 'name', 'text'),
79
+ ('passkey', 'public_key', 'text'),
80
+ ('passkey', 'user_id', 'text'),
81
+ ('passkey', 'credential_id', 'text'),
82
+ ('passkey', 'counter', 'int4'),
83
+ ('passkey', 'device_type', 'text'),
84
+ ('passkey', 'backed_up', 'bool'),
85
+ ('passkey', 'transports', 'text'),
86
+ ('passkey', 'created_at', 'timestamptz'),
87
+ ('passkey', 'aaguid', 'text'),
88
+ ('passkey', 'rp_id', 'text'),
89
+ ('sign_in_policy', 'id', 'text'),
90
+ ('sign_in_policy', 'password', 'text'),
91
+ ('sign_in_policy', 'email_code', 'text'),
92
+ ('sign_in_policy', 'passkey', 'text'),
93
+ ('sign_in_policy', 'two_factor', 'text'),
94
+ ('sign_in_policy', 'staff_access', 'text'),
95
+ ('sign_in_policy', 'platform_sign_in', 'text'),
96
+ ('sign_in_policy', 'created_at', 'timestamptz'),
97
+ ('sign_in_policy', 'created_by', 'text'),
98
+ ('staff_sign_in', 'id', 'text'),
99
+ ('staff_sign_in', 'user_id', 'text'),
100
+ ('staff_sign_in', 'user_email', 'text'),
101
+ ('staff_sign_in', 'staff', 'text'),
102
+ ('staff_sign_in', 'staff_name', 'text'),
103
+ ('staff_sign_in', 'staff_subject', 'text'),
104
+ ('staff_sign_in', 'issuer', 'text'),
105
+ ('staff_sign_in', 'reason', 'text'),
106
+ ('staff_sign_in', 'session_id', 'text'),
107
+ ('staff_sign_in', 'started_at', 'timestamptz'),
108
+ ('staff_sign_in', 'expires_at', 'timestamptz'),
109
+ ('staff_sign_in', 'ended_at', 'timestamptz'),
110
+ ('invitation', 'id', 'text'),
111
+ ('invitation', 'organization_id', 'text'),
112
+ ('invitation', 'email', 'text'),
113
+ ('invitation', 'role', 'text'),
114
+ ('invitation', 'status', 'text'),
115
+ ('invitation', 'expires_at', 'timestamptz'),
116
+ ('invitation', 'created_at', 'timestamptz'),
117
+ ('invitation', 'inviter_id', 'text'),
118
+ ('invitation', 'app_roles', 'jsonb'),
119
+ ('member', 'id', 'text'),
120
+ ('member', 'organization_id', 'text'),
121
+ ('member', 'user_id', 'text'),
122
+ ('member', 'role', 'text'),
123
+ ('member', 'created_at', 'timestamptz'),
124
+ ('member', 'app_roles', 'jsonb'),
125
+ ('organization', 'id', 'text'),
126
+ ('organization', 'name', 'text'),
127
+ ('organization', 'slug', 'text'),
128
+ ('organization', 'logo', 'text'),
129
+ ('organization', 'created_at', 'timestamptz'),
130
+ ('organization', 'metadata', 'text'),
131
+ ('session', 'active_organization_id', 'text'),
132
+ ('user', 'last_organization_id', 'text')) as expected (table_name, column_name, udt_name)
133
+ where not exists (
134
+ select from information_schema.columns found
135
+ where found.table_schema = 'auth' and found.table_name = expected.table_name and found.column_name = expected.column_name and found.udt_name = expected.udt_name
136
+ );
137
+ if missing is not null then
138
+ raise exception 'The auth store does not match the migrations this file adopts: it lacks %, or has them as other types', missing;
139
+ end if;
140
+ end
141
+ $adopt$;
142
+
143
+ insert into "auth"."auth_migrations" (version, name) values (1, 'core'), (2, 'two_factor'), (3, 'passkeys'), (4, 'sign_in_policy'), (5, 'staff_sign_in'), (6, 'organizations');
144
+
145
+ -- 7 mcp: The clients, resources, tokens and consents of the OAuth provider for MCP.
146
+ create table "auth"."oauth_client" (
147
+ id text primary key,
148
+ client_id text not null unique,
149
+ client_secret text,
150
+ client_discovery_id text,
151
+ disabled boolean,
152
+ skip_consent boolean,
153
+ enable_end_session boolean,
154
+ subject_type text,
155
+ scopes jsonb,
156
+ client_credentials_scopes jsonb,
157
+ user_id text references "auth"."user" (id) on delete cascade,
158
+ created_at timestamptz,
159
+ updated_at timestamptz,
160
+ name text,
161
+ uri text,
162
+ icon text,
163
+ contacts jsonb,
164
+ tos text,
165
+ policy text,
166
+ software_id text,
167
+ software_version text,
168
+ software_statement text,
169
+ redirect_uris jsonb not null,
170
+ post_logout_redirect_uris jsonb,
171
+ backchannel_logout_uri text,
172
+ backchannel_logout_session_required boolean,
173
+ token_endpoint_auth_method text,
174
+ application_type text,
175
+ jwks text,
176
+ jwks_uri text,
177
+ grant_types jsonb,
178
+ response_types jsonb,
179
+ require_pkce boolean,
180
+ dpop_bound_access_tokens boolean,
181
+ reference_id text,
182
+ metadata jsonb
183
+ );
184
+
185
+ create index oauth_client_user_id_idx on "auth"."oauth_client" (user_id);
186
+
187
+ create table "auth"."oauth_resource" (
188
+ id text primary key,
189
+ identifier text not null unique,
190
+ name text not null,
191
+ access_token_ttl integer,
192
+ refresh_token_ttl integer,
193
+ signing_algorithm text,
194
+ signing_key_id text,
195
+ allowed_scopes jsonb,
196
+ custom_claims jsonb,
197
+ dpop_bound_access_tokens_required boolean,
198
+ disabled boolean,
199
+ created_at timestamptz,
200
+ updated_at timestamptz,
201
+ policy_version integer,
202
+ metadata jsonb
203
+ );
204
+
205
+ create table "auth"."oauth_client_resource" (
206
+ id text primary key,
207
+ client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
208
+ resource_id text not null references "auth"."oauth_resource" (identifier) on delete cascade,
209
+ metadata jsonb,
210
+ created_at timestamptz
211
+ );
212
+
213
+ create index oauth_client_resource_client_id_idx on "auth"."oauth_client_resource" (client_id);
214
+
215
+ create index oauth_client_resource_resource_id_idx on "auth"."oauth_client_resource" (resource_id);
216
+
217
+ create unique index oauth_client_resource_client_id_resource_id_uidx on "auth"."oauth_client_resource" (client_id, resource_id);
218
+
219
+ create table "auth"."oauth_refresh_token" (
220
+ id text primary key,
221
+ token text not null unique,
222
+ client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
223
+ session_id text references "auth"."session" (id) on delete set null,
224
+ user_id text not null references "auth"."user" (id) on delete cascade,
225
+ reference_id text,
226
+ authorization_code_id text,
227
+ resources jsonb,
228
+ requested_user_info_claims jsonb,
229
+ expires_at timestamptz not null,
230
+ created_at timestamptz not null,
231
+ revoked timestamptz,
232
+ rotated_at timestamptz,
233
+ rotation_replay_response text,
234
+ rotation_replay_expires_at timestamptz,
235
+ auth_time timestamptz,
236
+ confirmation jsonb,
237
+ scopes jsonb not null
238
+ );
239
+
240
+ create index oauth_refresh_token_client_id_idx on "auth"."oauth_refresh_token" (client_id);
241
+
242
+ create index oauth_refresh_token_session_id_idx on "auth"."oauth_refresh_token" (session_id);
243
+
244
+ create index oauth_refresh_token_user_id_idx on "auth"."oauth_refresh_token" (user_id);
245
+
246
+ create index oauth_refresh_token_authorization_code_id_idx on "auth"."oauth_refresh_token" (authorization_code_id);
247
+
248
+ create table "auth"."oauth_access_token" (
249
+ id text primary key,
250
+ token text not null unique,
251
+ client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
252
+ session_id text references "auth"."session" (id) on delete set null,
253
+ user_id text references "auth"."user" (id) on delete cascade,
254
+ reference_id text,
255
+ authorization_code_id text,
256
+ resources jsonb,
257
+ requested_user_info_claims jsonb,
258
+ refresh_id text references "auth"."oauth_refresh_token" (id) on delete cascade,
259
+ expires_at timestamptz not null,
260
+ created_at timestamptz not null,
261
+ revoked timestamptz,
262
+ confirmation jsonb,
263
+ scopes jsonb not null
264
+ );
265
+
266
+ create index oauth_access_token_client_id_idx on "auth"."oauth_access_token" (client_id);
267
+
268
+ create index oauth_access_token_session_id_idx on "auth"."oauth_access_token" (session_id);
269
+
270
+ create index oauth_access_token_user_id_idx on "auth"."oauth_access_token" (user_id);
271
+
272
+ create index oauth_access_token_authorization_code_id_idx on "auth"."oauth_access_token" (authorization_code_id);
273
+
274
+ create index oauth_access_token_refresh_id_idx on "auth"."oauth_access_token" (refresh_id);
275
+
276
+ create table "auth"."oauth_consent" (
277
+ id text primary key,
278
+ client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
279
+ user_id text references "auth"."user" (id) on delete cascade,
280
+ reference_id text,
281
+ resources jsonb,
282
+ requested_user_info_claims jsonb,
283
+ scopes jsonb not null,
284
+ created_at timestamptz not null,
285
+ updated_at timestamptz not null
286
+ );
287
+
288
+ create index oauth_consent_client_id_idx on "auth"."oauth_consent" (client_id);
289
+
290
+ create index oauth_consent_user_id_idx on "auth"."oauth_consent" (user_id);
291
+
292
+ create table "auth"."oauth_client_assertion" (
293
+ id text primary key,
294
+ expires_at timestamptz not null
295
+ );
296
+
297
+ insert into "auth"."auth_migrations" (version, name) values (7, 'mcp');
@@ -1,6 +1,6 @@
1
1
  import { describe, expect, it } from 'vitest'
2
2
  import type { WriteEvent } from '@protobase/server'
3
- import { overdueCheck } from './jobs'
3
+ import { overdueCheck } from './schedule-overdue-check'
4
4
 
5
5
  const event = (operation: WriteEvent['operation'], after?: Record<string, unknown>, resource = 'invoices') =>
6
6
  ({ operation, resource: { name: resource }, user: { id: 1, roles: [] }, ...(after && { after }) }) as unknown as WriteEvent
@@ -0,0 +1,16 @@
1
+ import { enqueueJob, type PipelineHook, type WriteEvent } from '@protobase/server'
2
+
3
+ const day = 86_400_000
4
+
5
+ /** For a sent invoice, the check that it is paid: the day after it is due, one waiting check per invoice. */
6
+ export const overdueCheck = (event: WriteEvent) => {
7
+ if (event.resource.name !== 'invoices' || event.operation === 'delete' || event.after?.status !== 'sent') return undefined
8
+ const invoiceId = String(event.after.id)
9
+ return { invoiceId, runAt: new Date(Date.parse(String(event.after.due_at)) + day), key: `invoice:${invoiceId}` }
10
+ }
11
+
12
+ // In the write's own transaction: the invoice and the check of its payment commit together or not at all.
13
+ export default (async (event, trx) => {
14
+ const check = overdueCheck(event)
15
+ if (check) await enqueueJob(trx, 'mark-invoice-overdue', { invoiceId: check.invoiceId }, { runAt: check.runAt, key: check.key, mode: 'replace' })
16
+ }) satisfies PipelineHook
@@ -0,0 +1,7 @@
1
+ import { sql } from 'kysely'
2
+ import type { JobRun } from '@protobase/server'
3
+
4
+ // Declared in protobase.config.ts: an invoice still sent and unpaid once its due date has passed becomes overdue.
5
+ export default (async ({ invoiceId }, { db }) => {
6
+ await sql`update sales.invoices set status = 'overdue' where id = ${invoiceId} and status = 'sent' and due_at < current_date`.execute(db)
7
+ }) satisfies JobRun<{ invoiceId: string }>
@@ -0,0 +1,8 @@
1
+ import { sql } from 'kysely'
2
+ import type { JobRun } from '@protobase/server'
3
+
4
+ // Declared in protobase.config.ts: every night, the sent invoices that passed their due date, such as imported ones
5
+ // that never had a check enqueued.
6
+ export default (async (_, { db }) => {
7
+ await sql`update sales.invoices set status = 'overdue' where status = 'sent' and due_at < current_date`.execute(db)
8
+ }) satisfies JobRun
@@ -16,11 +16,12 @@
16
16
  "dev": "protobase dev"
17
17
  },
18
18
  "dependencies": {
19
- "@protobase/cli": "^0.47.1",
20
- "@protobase/layout": "^0.47.1",
21
- "@protobase/schema": "^0.47.1",
22
- "@protobase/server": "^0.47.1",
23
- "@protobase/ui": "^0.47.1",
19
+ "@protobase/cli": "^0.49.0",
20
+ "@protobase/config": "^0.49.0",
21
+ "@protobase/layout": "^0.49.0",
22
+ "@protobase/schema": "^0.49.0",
23
+ "@protobase/server": "^0.49.0",
24
+ "@protobase/ui": "^0.49.0",
24
25
  "better-auth": "^1.7.12",
25
26
  "jose": "^6.2.12",
26
27
  "kysely": "^0.29.6",
@@ -1,22 +1,37 @@
1
- import { defineConfig } from '@protobase/server'
2
- import * as config from './config'
3
- import * as jobs from './config/jobs'
4
- import { roles } from './config/roles'
5
- import { auth, authenticate } from './auth/auth'
1
+ import { defineConfig } from '@protobase/config'
6
2
 
3
+ // The whole app, as one object. Its code is found by its path: resources in config/, roles in config/roles.ts, each
4
+ // job's run in jobs/<name>.ts, each write hook in hooks/<name>.ts, the auth hooks in auth/hooks.ts and the API
5
+ // functions in functions/. Its address and secret are Protobase's defaults, PROTOBASE_URL and PROTOBASE_SECRET (see
6
+ // .env.example).
7
7
  export default defineConfig({
8
- config,
9
- auth,
10
- authenticate,
11
- jobs,
12
- options: {
13
- roles,
14
- writeHooks: [
15
- // In the write's own transaction: the invoice and the check of its payment commit together or not at all.
16
- async (event, trx) => {
17
- const check = jobs.overdueCheck(event)
18
- if (check) await jobs.markInvoiceOverdue.enqueue(trx, { invoiceId: check.invoiceId }, { runAt: check.runAt, key: check.key, mode: 'replace' })
19
- },
20
- ],
8
+ // The time zone of the jobs' schedules, such as the nightly overdue check.
9
+ timezone: 'Europe/Amsterdam',
10
+ auth: {
11
+ // `admin` is the superuser, in every organization; the others are held per organization (or globally, by a few).
12
+ roles: {
13
+ admin: 'Superuser',
14
+ auditor: 'Auditor',
15
+ manager: { label: 'Manager', grants: ['sales', 'accountant', 'warehouse', 'editor'] },
16
+ sales: 'Sales',
17
+ accountant: 'Accountant',
18
+ warehouse: 'Warehouse',
19
+ editor: 'Editor',
20
+ integration: 'Integration',
21
+ },
22
+ // New users and organizations take their ids from the ERP's own sequences (auth/hooks.ts).
23
+ organizations: true,
21
24
  },
25
+ jobs: {
26
+ 'mark-invoice-overdue': {
27
+ description: 'An invoice still sent and unpaid once its due date has passed becomes overdue.',
28
+ arguments: { invoiceId: 'bigint' },
29
+ },
30
+ 'mark-overdue-invoices': {
31
+ description: 'Every night, the sent invoices that passed their due date, such as imported ones that never had a check enqueued.',
32
+ schedule: '0 2 * * *',
33
+ },
34
+ },
35
+ // A sent invoice gets the check of its payment, in the write's own transaction.
36
+ writeHooks: ['schedule-overdue-check'],
22
37
  })
@@ -3,7 +3,7 @@
3
3
  DATABASE_URL=postgres://protobase:protobase@localhost:55432/real_estate
4
4
 
5
5
  # Admin store (Better Auth users, sessions, signing keys): the `auth` schema of DATABASE_URL, created by `db:migrate`.
6
- BETTER_AUTH_URL=http://localhost:5173
6
+ PROTOBASE_URL=http://localhost:5173
7
7
  # Signs cookies and encrypts the token keys. Generate your own: openssl rand -base64 32
8
- BETTER_AUTH_SECRET=change-me-generate-with-openssl-rand-base64-32
9
- # TRUSTED_ORIGINS=
8
+ PROTOBASE_SECRET=change-me-generate-with-openssl-rand-base64-32
9
+ # PROTOBASE_TRUSTED_ORIGINS=
@@ -0,0 +1,9 @@
1
+ import { sql } from 'kysely'
2
+ import type { AuthHooks } from '@protobase/server'
3
+
4
+ // Tickets name their assignee by core.users' integer id, so a new user takes the next value of that table's sequence;
5
+ // their row there comes with the account (db/migrations/013_auth_users.sql).
6
+ export const generateUserId: AuthHooks['generateUserId'] = async ({ db }) => {
7
+ const { rows } = await sql<{ id: string }>`select nextval(pg_get_serial_sequence('core.users', 'id'))::text as id`.execute(db)
8
+ return rows[0]!.id
9
+ }